[C++] DP(Dynamic Programming)

leeactยท2023๋…„ 5์›” 29์ผ
1

c++ ์ •๋ฆฌ

๋ชฉ๋ก ๋ณด๊ธฐ
10/13
post-thumbnail

๐Ÿ“• DP

ํ•˜๋‚˜์˜ ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„์–ด ํ’€๊ณ  ๊ฒฐํ•ฉํ•˜์—ฌ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•

โœ” ์ „์ œ ์กฐ๊ฑด

  1. ๊ฐ™์€ ๊ทœ์น™์œผ๋กœ ๊ณ„์‚ฐํ•ด ๋‚˜๊ฐ€์•ผ ํ•œ๋‹ค.
  2. ํ•œ ๋ฒˆ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.

โœ” ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

  1. ์ƒํ™ฉ์„ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€์ธ ์š”์ธ ์ฐพ๊ธฐ
  2. ๋ฐฐ์—ด ์„ค์ •(๋ณ€์ธ ์š”์ธ n: n์ฐจ์›)
  3. ๊ทœ์น™ ์ฐพ๊ธฐ
๋ฐฐ์—ด์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ

int arr[10] = {1, 2, 3, 4, 5, 6, 7,  8, 9, 10};
for (int index = 0; index < 10; index++){
	for (int i = 0; i < index; i++){
    	prefix[index] += arr[i];
    }
}
---
DP
prefix[0] = 1;
for (int i = 1; i<10; i++){
	prefix[i] = prefix[i-1] + arr[i];
}
     

๐Ÿ“ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

โœ” Bottom-Up

  • ๊ฐ€์žฅ ์ž‘์€ ๋ฌธ์ œ๋ถ€ํ„ฐ ์ง„ํ–‰
  • ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ตฌํ˜„

โœ” Top-down

  • ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ๋ถ€ํ„ฐ ์ง„ํ–‰
  • ์‹œ์ž‘์—์„œ ๋ ๊ฐ”๋‹ค๊ฐ€ ๋์—์„œ ๋‹ค์‹œ return ํ•ด์˜จ๋‹ค.
  • ์žฌ๊ท€ ํ˜ธ์ถœ(dfs)๋กœ ๊ตฌํ˜„

Bottom-Up๊ณผ Top-down ๋‘˜ ๋‹ค ๊ฐ๊ฐ์˜ ์žฅ๋‹จ์ ์ด ์žˆ๊ธฐ์— ์–ด๋–ค ๋ฐฉ๋ฒ•์ด ๋” ์ข‹๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์—†๋‹ค.

(DP์—์„œ ์ดˆ๊ธฐ๊ฐ’์€ ๊ณ„์‚ฐ์ƒ "์ ˆ๋Œ€ ๋‚˜์˜ฌ ์ˆ˜ ์—†๋Š” ์ˆ˜" ์„ค์ •)

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 5์›” 30์ผ

DP๊นŒ์ง€ ์ •๋ณตํ•˜์‹œ๋‹ค๋‹ˆ,,! ๋Œ€๋‹จํ•ด์š” ๋ฆฌ์•กํŠธ์”จ!!!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ