RGB Street

๋‚ด ์ ‘๊ทผ ๋ฐฉ๋ฒ• : 1์ฐจ์› DP

d[n] = n๊ฐœ์˜ ์ง‘ ์ƒ‰์น  ์ตœ์†Œ ๋น„์šฉ => n๋ฒˆ์งธ ์ง‘ ์ƒ‰์ด 0์ธ์ง€, 1์ธ์ง€, 2์ธ์ง€์— ๋”ฐ๋ผ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋ฉด ์ •๋‹ต์ด ๋œ๋‹ค! d[n] = Math.min(d[n-1]+P[0] , d[n-1]+P[1] , d[n-1]+P[2])

์ •๋‹ต ์ฐพ๋Š” ์ ‘๊ทผ ๋ฐฉ๋ฒ•์€ ๋งž์•˜๋‹ค! ๊ทธ๋Ÿฐ๋ฐ ์ •๋‹ต์—๋Š” 2์ฐจ์› DP๋ฅผ ์ •์˜ํ•ด์„œ n๋ฒˆ์งธ ์ง‘์˜ ์ƒ‰์„ ํ•จ๊ป˜ ๊ธฐ๋กํ•ด์ฃผ์—ˆ๋‹ค! d[n] = Math.min(d[n][0], d[n][1], d[n][2]) d[n][0] = Math.min(d[n-1][1],d[n-1][2]) + P[n][0] d[n][1] = Math.min(d[n-1][0],d[n-1][2]) + P[n][1] d[n][2] = Math.min(d[n-1][0],d[n-1][1]) + P[n][2]

์—ฌ๊ธฐ์„œ d[n-1]์ด๋ผ๊ณ  ๋‹จ์ˆœํžˆ ๊ณ„์‚ฐํ–ˆ๋Š”๋ฐ, ์‚ฌ์‹ค n๋ฒˆ์งธ ์ง‘ ์ƒ‰์ด 0 ์ด๋ผ๋ฉด n-1์€ 1 ๋˜๋Š” 2 ์ค‘ ์ตœ์†Ÿ๊ฐ’์ด ๋œ๋‹ค!!! ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ n=1 ) n-1 = 0 ๋˜๋Š” 2 ์ค‘ ์ตœ์†Ÿ๊ฐ’ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ n=2 ) n-1 = 0 ๋˜๋Š” 1 ์ค‘ ์ตœ์†Ÿ๊ฐ’

n-1๊ณผ n-1 = 0 ๋˜๋Š” 1 ์ค‘ ์ตœ์†Ÿ๊ฐ’์€ ๋‹ค๋ฅด๋‹ค.

Last updated