๐Ÿ’ป [์ฝ”ํ…Œ10] 15์žฅ ๋™์  ๊ณ„ํš๋ฒ•

๊น€๋ฏธ์—ฐยท2024๋…„ 3์›” 24์ผ
0

15์žฅ. ๋™์  ๊ณ„ํš๋ฒ•

15-1. ๋™์  ๊ณ„ํš๋ฒ•(Dynamic Programming) ๊ฐœ๋…

  • ์ „์ฒด ๋ฌธ์ œ๋ฅผ ํ•œ ๋ฒˆ์— ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ž‘์€ ๋ถ€๋ถ„ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•˜๊ณ  ์ด๊ฒƒ๋“ค์„ ํ™œ์šฉํ•˜์—ฌ ์ „์ฒด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ๋™์  ๊ณ„ํš๋ฒ•์„ ํšจ์œจ์ ์œผ๋กœ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ถฉ์กฑ ๋˜์–ด์•ผ ํ•˜๋Š” ์กฐ๊ฑด
    • ํฐ ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋™์ผํ•œ ์ž‘์€ ๋ฌธ์ œ ๋ฐ˜๋ณต ๋“ฑ์žฅ
    • ํฐ ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ์ฑ…์€ ์ž‘์€ ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ์ฑ…์˜ ํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ
  • ์ตœ์  ๋ถ€๋ถ„ ๊ตฌ์กฐ(opimal substructure)
    : ์ž‘์€ ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ์ฑ…์˜ ํ•ฉ์œผ๋กœ ํฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ
  • ์ค‘๋ณต ๋ถ€๋ถ„ ๋ฌธ์ œ(overlapping subproblems)
    : ํฐ ๋ฌธ์ œ๋ฅผ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ์ž‘์€ ๋ฌธ์ œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ œ

1) ์ ํ™”์‹ ์„ธ์šฐ๊ธฐ์™€ ๋™์  ๊ณ„ํš๋ฒ•

  • ์ ˆ์ฐจ1) ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ํ•ด๊ฐ€ ์ด๋ฏธ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค
  • ์ ˆ์ฐจ2) ์ข…๋ฃŒ ์กฐ๊ฑด์„ ์„ค์ •ํ•œ๋‹ค
  • ์ ˆ์ฐจ3) ์ ˆ์ฐจ 1, 2๋ฅผ ํ™œ์šฉํ•ด ์ ํ™”์‹์„ ์„ธ์šด๋‹ค
    - ์ ํ™”์‹ ๊ตฌํ˜„ : ์žฌ๊ท€ ํ™œ์šฉ
    • ์žฌ๊ท€ ํ˜ธ์ถœ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• : ๋ฐ˜๋ณต๋ฌธ ๋˜๋Š” ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํ™œ์šฉ

2) ์žฌ๊ท€ ํ˜ธ์ถœ์˜ ํšŸ์ˆ˜๋ฅผ ์ค„์—ฌ์ฃผ๋Š” ๋ฉ”๋ชจ์ด์ œ์ด์…˜

  • ๋ฉ”๋ชจ์ด์ œ์ด์…˜ : ์ด๋ฏธ ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ์ €์žฅํ•ด๋‘์—ˆ๋‹ค๊ฐ€ ์ด ํ›„ ์“ธ ์ผ์ด ์žˆ์„ ๋•Œ ํ™œ์šฉ
  • ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ ๊ตฌํ•˜๊ธฐ ๋ฌธ์ œ : ์ตœ์  ๋ถ€๋ถ„ ๊ตฌ์กฐ์ด๋ฉด์„œ ์ค‘๋ณต ๋ถ€๋ถ„ ๋ฌธ์ œ์— ํ•ด๋‹น
    - ์ ํ™”์‹ ๊ตฌํ˜„ : ์žฌ๊ท€ + ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํ™œ์šฉ

3) ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด

  • ๋ถ€๋ถ„ ์ˆ˜์—ด : ์ฃผ์–ด์ง„ ์ˆญ๋ ค ์ค‘ ์ผ๋ถ€๋ฅผ ๋ฝ‘์•„ ์ƒˆ๋กœ ๋งŒ๋“  ์ˆ˜์—ด(๋‹จ, ๊ฐ๊ฐ์˜ ์šด์†Œ๋Š” ์ €ํ›„ ๊ด€๊ณ„ ์œ ์ง€ ํ•„์ˆ˜)
  • ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด(Longest Increasing Subsequence;LIS) : ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ์›์†Œ๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์„ ์œ ์ง€ํ•˜๋ฉด์„œ๋„ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ๊ธด ์ˆ˜์—ด
  • LIS์˜ ๊ธธ์ด ๋™์  ๊ณ„ํš๋ฒ•์œผ๋กœ ๊ตฌํ•˜๊ธฐ

4) ์ตœ์žฅ ๊ณตํ†ต ๋ถ€๋ถ„ ์ˆ˜์—ด

  • Longest Common Subsequence(LCS)
  • ๋‘ ์ˆ˜์—ด์ด ์–ด๋–ค ๊ธฐ์ค€์— ๋”ฐ๋ผ ์–‘์ชฝ์—์„œ ๊ณตํ†ต์œผ๋กœ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๊ธด ๋ถ€๋ถ„ ์ˆ˜์—ด
  • LCS์˜ ๊ธธ์ด ๋™์  ๊ณ„ํš๋ฒ•์œผ๋กœ ๊ตฌํ•˜๊ธฐ

15-2. ๋ชธํ’€๊ธฐ ๋ฌธ์ œ

  • ๋ฌธ์ œ70_LCS ๊ธธ์ด ๊ณ„์‚ฐํ•˜๊ธฐ
'''
<์ตœ์žฅ ๊ณตํ†ต ๋ถ€๋ถ„ ์ˆ˜์—ด ๊ตฌํ•˜๊ธฐ>
1. dp 2์ฐจ์› ๋ฐฐ์—ด ์ค€๋น„
2. dp(0, 0) = 0
3. str1[i] == str2[j] >> dp(i, j) = dp(i-1, j-1) + 1
4. str1[i] != str2[j] >> max(dp(i-1, j), dp(i, j-1))

'''


def solution(str1, str2):
    n = len(str1)
    m = len(str2)
    dp = [[0] * (m+1) for _ in range(n+1)]
    
    for i in range(0, n):
        for j in range(0, m):
            if str1[i] == str2[j]:
                dp[i+1][j+1] = dp[i][j] + 1
            else:
                dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])
    
    return max(max(dp))

# TEST ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. ์ฃผ์„์„ ํ’€๊ณ  ์‹คํ–‰์‹œ์ผœ๋ณด์„ธ์š”
print(solution("ABCBDAB","BDCAB")) # ๋ฐ˜ํ™˜๊ฐ’ : 4
print(solution("AGGTAB","GXTXAYB")) # ๋ฐ˜ํ™˜๊ฐ’ : 4
  • ๋ฌธ์ œ71_LIS ๊ธธ์ด ๊ณ„์‚ฐํ•˜๊ธฐ
'''
<์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด ๊ธธ์ด ๊ตฌํ•˜๊ธฐ>
>> ํ•ด๋‹น ์ˆซ์ž๋กœ ๋๋‚˜๋Š” LIS ๊ธธ์ด ๋„ฃ๊ธฐ

0. n ๊ธธ์ด์˜ result ์ดˆ๊ธฐํ™”(0์œผ๋กœ)
1. result[0] = 1
2. ์ˆ˜์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ์ˆœํšŒ
    1) ํ˜„์žฌ ๊ฐ’ ์ด์ „ ๊ฐ’ ์ˆœํšŒ
        ใ„ฑ) ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๊ฐ’๋“ค ์ค‘ LIS ๊ธธ์ด ์ตœ๋Œ€์น˜ + 1
'''

def solution(nums):
    n = len(nums)
    lengths = [1] * n
    for i in range(1, n):
        for j in range(i):
            if nums[i] > nums[j]:
                lengths[i] = max(lengths[i], lengths[j] + 1)
    return max(lengths)

# TEST ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. ์ฃผ์„์„ ํ’€๊ณ  ์‹คํ–‰์‹œ์ผœ๋ณด์„ธ์š”
print(solution([1, 4, 2, 3, 1, 5, 7, 3])) # ๋ฐ˜ํ™˜๊ฐ’ : 5
print(solution([3, 2, 1])) # ๋ฐ˜ํ™˜๊ฐ’ : 1
  • ๋ฌธ์ œ72_์กฐ์•ฝ๋Œ ๋ฌธ์ œ
def solution(arr):
    m = len(arr[0])
    dp = [[0] * m for _ in range(4)]
    
    dp[0][0] = arr[0][0]
    dp[1][0] = arr[1][0]
    dp[2][0] = arr[2][0]
    dp[3][0] = arr[0][0] + arr[2][0]
    
    for i in range(1, m):
        dp[0][i] = arr[0][i] + max(dp[1][i-1], dp[2][i-1])
        dp[1][i] = arr[1][i] + max(dp[0][i-1], dp[2][i-1], dp[3][i-1])
        dp[2][i] = arr[2][i] + max(dp[0][i-1], dp[1][i-1])
        dp[3][i] = arr[0][i] + arr[2][i] + dp[1][i-1]
    return max(dp[0][-1], dp[1][-1], dp[2][-1], dp[3][-1])



# TEST ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. ์ฃผ์„์„ ํ’€๊ณ  ์‹คํ–‰์‹œ์ผœ๋ณด์„ธ์š”
print(solution([[1, 3, 3, 2], [2, 1, 4, 1], [1, 5, 2, 3]])) # ๋ฐ˜ํ™˜๊ฐ’ : 19
print(solution([[1, 7, 13, 2, 6], [2, -4, 2, 5, 4], [5, 3, 5, -3, 1]])) # ๋ฐ˜ํ™˜๊ฐ’ : 32

15-3. ํ•ฉ๊ฒฉ์ž๊ฐ€ ๋˜๋Š” ๋ชจ์˜ ํ…Œ์ŠคํŠธ

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