Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
๐ ๋ฌธ์ ๋งํฌ
๊ตฌ๊ฐํฉ๊ตฌํ๊ธฐ5
โ๏ธ ์์๋ ์๊ฐ
ํ๋ฃจ
์ฒซ๋ฒ์งธํ์ด
์ฒซ๋ฒ์งธํ์ด๋
๋ฐฐ์ด
์ํตํด ํ๋ฅผ์ ๋ ฅ๋ฐ๊ณ ๋ฌด์ํ๊ฒ๊ตฌ๊ฐํฉ์ ๋ฒ์๊ฐ ๋ค์ด์ฌ๋๋ง๋ค for๋ฃจํ๋ฌธ์ํตํด ๊ตฌ๊ฐ์ ๋ฒ์๋ฅผ ๋ค ํ์ํ๋ฉฐ ํฉ์ ๊ณ์ฐ
ํด์ฃผ์๋ค. ์ด๋ฌ๋๋ ์ต์ ์ ๊ฒฝ์ฐO(cnt*size^2)
์ ์๊ฐ๋ณต์ก๋๊ฐ ๋ฐ์ํ๋ค. ์ด ๊ฒฝ์ฐ์ฐ์ฐ์ ์ต๋ํ์(cnt)
๊ฐ 100,000๊น์ง ์ธ๊ฑธ ๊ฐ์ํ๋ฉด ์์ฒญ๋ ์๊ฐ๋ณต์ก๋๊ฐ ๋ ๋ค. ๊ทธ๋์ ๋ค๋ฅธ๋ฐฉ๋ฒ๋ค์ ์ฐพ์๋ณด๋คdp
๋ฅผ ์ด์ฉํ์ฌ ํ๊ฒ๋์๋ค.๋๋ฒ์จฐํ์ด
์ดํ์ด์์๋ dp[i][j]๋ฅผ 1ํ1์ด์์ iํj์ด๊น์ง์ ๋ถ๋ถํฉ์ด๋ผ๊ณ ์ ์ํด์ฃผ์๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ์ ์ ์ฅํด์คฌ๋ค ์ฌํ์ฉํด์ฃผ๊ธฐ ์ํด
์ ํ์์ dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+temp(์ ๋ ฅ๋ฐ์๊ฐ) ์ผ๋ก ์ธ์์คฌ๋ค.
์์๊ฐ์ ๋ฐฐ์ด์์
dp[3][3]
์ ๊ตฌํ๋ ค๋ฉด ์ ํ์๊ณผ ๊ฐ์ดํ๋์ ๋ญ์น 2๊ฐ
๋ฅผ๋ํด์ฃผ๊ณ์ค๋ณต๋๋๊ตฌ๊ฐ(๋นจ๊ฐ์์น )
์๋นผ์ค๋ค ๊ทธ๋ฆฌ๊ณ์ ๋ ฅ๋ฐ์๊ฐ์ธ 6
์ ๋ํด์ค๋ค.for๋ฃจํ๋ฌธ
์ ํตํด ๋ฐฐ์ด์ ํฌ๊ธฐ๋งํผdp
๊ฐ์ ๋ค ์ค์ ํด์คฌ์ผ๋ฉด ์ด์ ๊ตฌ๊ฐํฉ
์๊ตฌํ๊ธฐ์ํดstart_row(์์ํ),start_col(์์์ด),end_row(๋ํ),end_col(๋์ด)
์ ๋ฐ์๋ค๋ง์ฝ
๋ค๋ชจ๋ฐ์ค๊ฐ ์๋ 6์์ 3๊น์ง์ ๋ถ๋ถํฉ(6,5,2,5,2,3)
์ ๊ตฌํ๊ณ ์ํ๋ค๋ฉดdp[๋ํ][๋์ด](๋นจ๊ฐ์๋ค๋ชจ)-dp[์์ํ-1][๋์ด](๊ฐ๋กํ๋์๋ถ๋ถ)-dp[๋ํ][์์์ด-1](์ธ๋กํ๋์๋ถ๋ถ)+dp[์์ํ-1][์์์ด-1](์ค๋ณต๋๋นจ๊ฐ์๋ถ๋ถ)
์ ์์ ์คํํด์ฃผ๋ฉด ๊ตฌํ๊ณ ์ํ๋ ๊ฐ์ธ ๋ถ๋ถํฉ์ ๊ตฌํ ์ ์๋ค. ์ด ๊ฒฝ์ฐ ํ๋ฅผ์ ๋ ฅ๋ฐ์๋ dp๊ฐ์ ๋ฐ๋ก ์ค์ ํด์ค์ผ๋ก์จ ํ๋์ ๋ถ๋ถํฉ์ ๊ตฌํ ๋O(1)
๋ก ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค ๋ฐ๋ผ์ ์ ์ฒด ์๊ฐ๋ณต์ก๋๋O(size^2)
์ด ๋๋ค.๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
dp๋กํธ๋ ๋ฐฉ๋ฒ์
์ ํ์
์ ์ธ์ธ ๋ฐฉ๋ฒ์ด ๋ ์ค๋ฅด์ง์์๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํ๋ค.