ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90

๋ฌธ์ œ

n๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ์ž„์˜์˜ ์ˆ˜์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด ์ค‘ ์—ฐ์†๋œ ๋ช‡ ๊ฐœ์˜ ์ˆ˜๋ฅผ ์„ ํƒํ•ด์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ํ•ฉ ์ค‘ ๊ฐ€์žฅ ํฐ ํ•ฉ์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋‹จ, ์ˆ˜๋Š” ํ•œ ๊ฐœ ์ด์ƒ ์„ ํƒํ•ด์•ผ ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด์„œ 10, -4, 3, 1, 5, 6, -35, 12, 21, -1 ์ด๋ผ๋Š” ์ˆ˜์—ด์ด ์ฃผ์–ด์กŒ๋‹ค๊ณ  ํ•˜์ž. ์—ฌ๊ธฐ์„œ ์ •๋‹ต์€ 12+21์ธ 33์ด ์ •๋‹ต์ด ๋œ๋‹ค.

 

๋”๋ณด๊ธฐ

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ n(1 ≤ n ≤ 100,000)์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” n๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆ˜์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ˆ˜๋Š” -1,000๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ 1

10

10 -4 3 1 5 6 -35 12 21 -1

์ถœ๋ ฅ: 33

 

์˜ˆ์ œ 2

10

2 1 -4 3 4 -4 6 5 -5 1

์ถœ๋ ฅ: 14

 

๋ฌธ์ œ ๋งํฌ


๋ฌธ์ œ ์œ ํ˜•

DP


๋ฌธ์ œ ํ’€์ด

๋ฏธ๋ฆฌ ์‚ดํŽด๋ณด๋Š” ํ•ต์‹ฌ ํฌ์ธํŠธ โœ… 

i ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ๊นŒ์ง€์˜ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ์„ ๊ธฐ๋กํ•œ๋‹ค.  sums[i-1]

์ด ๊ธฐ๋กํ•œ ๊ฒƒ์„ ๋‹ค์Œ i ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ์—์„œ ์ด์šฉํ•ด i ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ๊นŒ์ง€์˜ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ์„ ๊ตฌํ•œ๋‹ค.

 

 

nums[i]๋Š” ์ฃผ์–ด์ง„ ๋ฐฐ์—ด,

sums[i]๋Š” i๋ฒˆ์งธ ์ธ๋ฑ์Šค๊นŒ์ง€ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ์ด ๋‹ด๊ธด ๋ฐฐ์—ด์ด๋‹ค.

 

i ๋ฒˆ์งธ ์ด์ „๊นŒ์ง€์˜ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ(sums[i-1])์— i ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ ๊ฐ’(nums[i]) ์„ ๋”ํ•ด๋‚˜๊ฐ„๋‹ค.

๋‹จ, ์ด์ „๊นŒ์ง€์˜ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ(sums[i-1])์ด 0๋ณด๋‹ค ์ž‘์œผ๋ฉด ์ด์ „๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ์„ ๋ฒ„๋ฆฌ๊ณ  โžก๏ธ i ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ ๊ฐ’(nums[i])์„ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ์œผ๋กœ ์ƒˆ๋กœ ์‹œ์ž‘ํ•œ๋‹ค.

์™œ๋ƒํ•˜๋ฉด  ์ด์ „๊นŒ์ง€์˜ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ๊ณผ ์ž์‹ ์„ ๋”ํ•œ ๊ฒƒ๋ณด๋‹ค, ์ž๊ธฐ ์ž์‹ (nums[i])์ด ๋” ํฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.  nums[i] > sums[i-1] + nums[i]

sums[i] = nums[i] +  (sums[i-1] if sums[i-1] > 0 else 0)

# ๋˜๋Š”

sums[i] = max(sums[i-1] + nums[i], nums[i])

 


ํ•˜๋‚˜ ํ•˜๋‚˜ ํ•ด๋ณด๊ธฐ

 

๐ŸŸฆ index=0์ผ ๋•Œ, ํ˜„์žฌ ์œ„์น˜ ๊ธฐ์ค€ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ์€ ์ž๊ธฐ ์ž์‹  sums[0] = 10


๐ŸŸฆ index=1์ผ ๋•Œ, ํ˜„์žฌ ์œ„์น˜ ๊ธฐ์ค€ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ์€ sums[i-1] + nums[i] = 10 + (-12) = -2   โžก๏ธ  sums[1] = -2


๐ŸŸฆ index=2์ผ ๋•Œ,  ์ด์ „๊นŒ์ง€์˜ ์—ฐ์† ์ตœ๋Œ€ํ•ฉ์ด 0๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ,  nums[2] > sums[1] + nums[2]  โžก๏ธ sums[2] = 30


๐ŸŸฆ index=3์ผ ๋•Œ, ํ˜„์žฌ ์œ„์น˜ ๊ธฐ์ค€ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ์€ sums[i-1] + nums[i] = 30+(-8)  โžก๏ธ sums[3] = 22

 

์ฆ‰, ํ˜„์žฌ ์œ„์น˜ ๊ธฐ์ค€ ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ์€ ์ด์ „ ์—ฐ์†ํ•ฉ๊ณผ ๋”ํ•˜๊ฑฐ๋‚˜ ๋”ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ 2๊ฐ€์ง€ ์ค‘ ํ•œ ๊ฐ€์ง€๊ฐ€ ๋œ๋‹ค.

 

์œ„๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ ํ™”์‹์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฉฐ,

๊ฐ ๋ถ€๋ถ„์—์„œ ์ด์ „์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ํ•ด๋‹น ์ตœ์  ๊ฒฐ๊ณผ๊ฐ’์ด ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๋˜๋‹ˆ 

DP์˜ ๋‘ ๊ฐ€์ง€ ์กฐ๊ฑด์ธ Overlapping Subproblems์™€ Optimal Substructure๋ฅผ ๋งŒ์กฑํ•œ๋‹ค.


DP ์‚ฌ์šฉํ•˜๊ธฐ

1๏ธโƒฃ DP๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ

๐Ÿ”ฝ

2๏ธโƒฃ ๋ฌธ์ œ์˜ ๋ณ€์ˆ˜ ํŒŒ์•…ํ•˜๊ธฐ

์ •์˜

: sums[i] = ์œ„์น˜ i์—์„œ์˜ ์—ฐ์†ํ•ฉ์˜ ์ตœ๋Œ€๊ฐ’

๐Ÿ”ฝ

3๏ธโƒฃ ์ ํ™”์‹ ๋งŒ๋“ค๊ธฐ

์ ํ™”์‹

sums[i] = nums[i] + (sums[i-1] if sums[i-1] > 0 else 0 )

๋˜๋Š”

sums[i] = max(sums[i-1] + arr[i], arr[i])

๐Ÿ”ฝ

4๏ธโƒฃ ๋ฉ”๋ชจํ•˜๊ธฐ(Memoization)

sums ๋ฐฐ์—ด์— ๋ฉ”๋ชจ๋˜๊ณ  ์žˆ๋‹ค.

๐Ÿ”ฝ

5๏ธโƒฃ ๊ธฐ์ € ์ƒํƒœ ํŒŒ์•…ํ•˜๊ธฐ

๊ธฐ์ € ์ƒํƒœ๋Š” ์ฒซ๋ฒˆ์งธ ์—ฐ์†ํ•ฉ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒฝ์šฐ์ผ ๊ฒƒ์ด๋‹ค. ์ œ์ผ ์ฒ˜์Œ์€ ๋‹จ์ˆœํžˆ ๊ทธ ์œ„์น˜์˜ ์› ๋ฐฐ์—ด์˜ ์ˆซ์ž๊ฐ€ ๋œ๋‹ค.

๊ธฐ์ € ์ƒํƒœ

: DP[0] = arr[0]

๐Ÿ”ฝ

6๏ธโƒฃ ๊ตฌํ˜„ํ•˜๊ธฐ

1) Bottom-Up (Tabulation ๋ฐฉ์‹) - ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ โœ…
2) Top-Down (Memoization ๋ฐฉ์‹) - ์žฌ๊ท€ ์‚ฌ์šฉ

 

์ „์ฒด ์ฝ”๋“œ

 

 

 

๊ธฐ์กด nums์— ์ตœ๋Œ€ ์—ฐ์†ํ•ฉ์„ ํ•จ๊ป˜ ๋„ฃ์–ด ๊ณต๊ฐ„์„ ์žฌํ™œ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. (๊ณต๊ฐ„ ๋ณต์žก๋„ ๐Ÿ”ฝ)

 

 

๊ฐ™์€ ์›๋ฆฌ์ธ ์•„๋ž˜์˜ ์ ํ™”์‹์œผ๋กœ๋„ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

 


์ฐธ๊ณ  ์ถœ์ฒ˜: [์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด - ๋ฐฑ์ค€ 1912(์—ฐ์†ํ•ฉ, DP) https://hongjw1938.tistory.com/61

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด - ๋ฐฑ์ค€ 1912(์—ฐ์†ํ•ฉ, DP)

๊ด€๋ จ๊ธ€ Dynamic Programming ๊ด€๋ จ ํฌ์ŠคํŒ…์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ์กฐ 1. ๊ฐœ์š” ๋ฌธ์ œ ๋งํฌ๋Š” ์—ฌ๊ธฐ๋ฅผ ์ฐธ์กฐ ๋ฌธ์ œ์˜ ๋‚ด์šฉ์„ ๋ณด๋ ค๋ฉด ์•„๋ž˜ ๋”๋ณด๊ธฐ ํด๋ฆญ ๋”๋ณด๊ธฐ ์ด ๋ฌธ์ œ๋Š” ๋ฐฐ์—ด์˜ ์—ฐ์†ํ•ฉ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. 2

hongjw1938.tistory.com

 

728x90
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ
250x250