๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปAlgorithm/์ด๋ก 

๋™์  ๊ณ„ํš ์•Œ๊ณ ๋ฆฌ์ฆ˜(DP/Dynamic Programming)

๋™์  ๊ณ„ํš ์•Œ๊ณ ๋ฆฌ์ฆ˜(DP)

 

์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ๋ถ€๋ถ„ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„์–ด์„œ ํ‘ผ ๋‹ค์Œ, ๊ทธ ๊ฒฐ๊ณผ๋กœ ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•

๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ์ •๋ณด๋ฅผ ์ €์žฅ->์‹œ๊ณต๊ฐ„์  ํšจ์œจ ์ฆ๊ฐ€

memoization ํ™œ์šฉ

์ตœ์  ๋ถ€๋ถ„ ๊ตฌ์กฐ(๊ธฐ๋ณธ ๋ฌธ์ œ์˜ ์ตœ์ ํ•ด๊ฐ€ ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ์ตœ์ ํ•ด๋ฅผ ํฌํ•จ)

 

 

DP ์ ์šฉ ์—ฌ๋ถ€ ํŒ๋‹จ ๋ฐฉ๋ฒ•

 

1.๋ฌธ์ œ๋ฅผ ๊ฐ™์€ ํ˜•ํƒœ์˜ ํ•˜์œ„ ๊ตฌ์กฐ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š”๊ฐ€

2.ํ•˜์œ„ ๋ฌธ์ œ์˜ ๊ณ„์‚ฐ์ด ๋ฐ˜๋ณต๋˜๋Š”๊ฐ€

(์ตœ์ ํ™”, ์ตœ๋Œ€ํ™”, ์ตœ์†Œํ™”, ์–ด๋–ก ์ž‘์—…์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ธ๊ฐ€ *์• ๋งคํ•œ ๊ฒฝ์šฐ์— ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€ํ‘œ์ ์ธ DP ๋ฌธ์ œ ์œ ํ˜•)

 

 

DP ์ ์šฉ ๊ณผ์ •

 

1. ์ ํ™”์‹ / ์žฌ๊ท€ ๊ณผ์ • ์ •์˜

 - ๋ฌธ์ œ๋ฅผ Top-Down(ํ•˜ํ–ฅ์‹/์žฌ๊ท€) ์œผ๋กœ ์ •์˜ํ•œ๋‹ค

 - ๋ฌธ์ œ ํ’€์ด์— ํ•„์š”ํ•œ ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ œ์˜ ํ•˜์œ„ ๋ถ€๋ถ„ ๊ฐ’์„ ์ดˆ๊ธฐํ™”

 - ์ข…๋ฃŒ ์กฐ๊ฑด ์ถ”๊ฐ€

2. memoization ์ ์šฉ

3. bottom-up(์ƒํ–ฅ์‹/๋ฐ˜๋ณต๋ฌธ) ์ฆ‰, DP ๋กœ ํ’€์ด

 

 

๋™์  ๊ณ„ํš๋ฒ•์˜ ์‚ฌ์šฉ

1. ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด

์žฌ๊ท€ ํ•จ์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๋ฉด f(1), f(2) ๋“ฑ ํ•˜์œ„ ๋ถ€๋ถ„์ด ๋ฐ˜๋ณต์ ์œผ๋กœ ๊ณ„์‚ฐ -> ๋น„ํšจ์œจ์ 

dp[i]=dp[i-1]+dp[i-2] ๋กœ ํ’€์ด ๊ฐ€๋Šฅ

 

 

2. ์—ญ๊ฐ„ ๊ฑฐ๋ฆฌ ์ตœ์†Ÿ๊ฐ’ ๋ฌธ์ œ

0~n-1 ๊นŒ์ง€ n๊ฐœ์˜ ์—ญ์ด ์กด์žฌํ•˜๊ณ  ํ•œ ๋ฐฉํ–ฅ(๋‹จ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„)์œผ๋กœ ์ด๋™ํ•  ๋–„, ์—ญ๋ถ€ํ„ฐ ์—ญ ์‚ฌ์ด ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

 

naiveํ•˜๊ฒŒ ์ ‘๊ทผํ•˜๋ฉด, 0์—์„œ 4๊นŒ์ง€ ์ด๋™ํ•˜๋ ค๊ณ  ํ•  ๋•Œ, (0,3)+(3,4) / (0,2)+(2,4) / (0,1)+(1,4) ์˜ ๊ฒฝ์šฐ๋ฅผ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ณ  ์ด๋Š” ๋˜ ((0,1)+(1,3))+(3,4) ๋“ฑ์œผ๋กœ ์ž‘๊ฒŒ ๋‚˜๋ˆ„์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. ๊ณ„์‚ฐ์˜ ๋ฐ˜๋ณต์ด ์žฆ๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ ํ™”๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

naiveํ•œ ํ’€์ด๋ฒ• ์ฝ”๋“œ

n=4

cost=[[0,10,75,94],[-1,0,35,50],[-1,-1,0,80],[-1,-1,-1,0]]

def minCost(s,d):
	if(s==d or s==d-1):
        return cost[s][d]

    min_value=cost[s][d]
    for i in range(s+1,d):
        temp=minCost(s,i)+minCost(i,d)
        min_value=min(temp,min_value)
    return min_value

print(minCost(0,3))

 

memoization์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ํ•˜์œ„ ๋ฌธ์ œ์˜ ๋ฐ˜๋ณต ๊ณ„์‚ฐ์„ ํ”ผํ•˜๋ฉด์„œ ๋ฌธ์ œ๋ฅผ ๊ตฌ์ฒดํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

memo๋ผ๋Š” 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ณ„์‚ฐํ•œ cost ๊ฐ’์„ ์ €์žฅํ•ด์ฃผ๊ณ  memo ๊ฐ’์ด ์žˆ์œผ๋ฉด ๋” ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๊ทธ ๊ฐ’์„ return ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

memoization ํ’€์ด๋ฒ• ์ฝ”๋“œ

n=4
cost=[[0,10,75,94],[-1,0,35,50],[-1,-1,0,80],[-1,-1,-1,0]]
memo=[[0]*4 for _ in range(n)]

def minCost(s,d):
    if s==d or s==d-1:
        return cost[s][d]
    if not memo[s][d]:
        value=cost[s][d]
        for i in range(s+1,d):
            temp=minCost(s,i)+minCost(i,d)
            value=min(value,temp)
        memo[s][d]=value

    return memo[s][d]

print(minCost(0,3))

memoization๋„ ์žฌ๊ท€ ํ˜ธ์ถœ์— ์˜ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋ชจ๋กœ ์ธํ•ด ์†๋„๊ฐ€ ๋Š๋ฆด ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด DP ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

dp ํ’€์ด๋ฒ• ์ฝ”๋“œ

n=4
cost=[[0,10,75,94],[-1,0,35,50],[-1,-1,0,80],[-1,-1,-1,0]]
mincost=[10**6]*n
mincost[0]=0
mincost[1]=cost[0][1]

for i in range(2,n):
    mincost[i]=cost[0][i]
    for j in range(1,i):
        mincost[i]=min(mincost[j]+cost[j][i],mincost[i])

print(*mincost)

 

 

3. ๋ฐฐ๋‚ญ ๋ฌธ์ œ

๊ฐ ๋ฌผ๊ฑด์˜ ๊ฐ€์ค‘์น˜์™€ ๋ฌด๊ฒŒ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€๋ฐฉ์— ์ตœ๋Œ€ํ•œ ๋งŽ์€ ๋ฌผ๊ฑด์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฌธ์ œ

-๋ฐฐ๋‚ญ์˜ ๋ฌด๊ฒŒ์™€ ๋ฌผ๊ฑด์˜ ๋ฌด๊ฒŒ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•œ๋‹ค

-๋ฐฐ๋‚ญ์— ์ƒˆ๋กœ ๋ฌผ๊ฑด์„ ๋„ฃ์ง€ ๋ชปํ•˜๋ฉด ์ด์ „ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ

 

์ด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด

w=[1,4,3]
v=[15,30,20]

def knapsack(w,v,n,c):
    if(n<=0 or c<=0):
        return 0
    if w[n-1]>c:
        return knapsack(w,v,n-1,c) 
    carry=v[n-1]+knapsack(w,v,n-1,c-w[n-1]) #์ง‘์–ด๋„ฃ์€ ๋ฌผ๊ฑด ๋งŒํผ ์šฉ๋Ÿ‰ ๋นผ์คŒ
    leave=knapsack(w,v,n-1,c) #๋ฌผ๊ฑด ๋„ฃ์ง€ ์•Š๊ณ  ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ
    print(carry,leave)
    return max(carry,leave)
    
print(knapsack(w,v,3,4))

์ด์™€ ๊ฐ™์ด ๊ตฌํ˜„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฅผ ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด, cell[i][j]์˜ max ๊ฐ’์€ 

-์ง€๊ธˆ ๊นŒ์ง€ ๊ตฌํ•œ cell[i-1][j]์˜ ๊ฐ’ ์ค‘ max ๊ฐ’

-ํ˜„์žฌ ๋ฌผ๊ฑด์˜ ๊ฐ€์น˜ + ๋‚จ์€ ๊ณต๊ฐ„์˜ ๊ฐ€์น˜(cell[i-1][j-๋ฌผ๊ฑด๋ฌด๊ฒŒ]) 

๋ฅผ ๋น„๊ตํ•œ ๊ฐ’์ด๋‹ค. 

 

DP๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ

w=[1,4,3]
v=[15,30,20]

def knapsack(w,v,n,c):
    cell=[[-1]*(c+1) for _ in range(n+1)]
    for i in range(n+1):
        cell[i][0]=0

    for j in range(c+1):
        cell[0][j]=0

    # cell#์ดˆ๊ธฐํ™”
    for i in range(1,n+1):
        for j in range(1,c+1):
            if w[i-1]<=j: #๋ฐฐ๋‚ญ ์•ˆ์— ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฉด
                x=j-w[i-1]
                ##๋‚จ๋Š” ๊ณต๊ฐ„
                cell[i][j]=max(cell[i-1][j],v[i-1]+cell[i-1][x])
            else:
                cell[i][j]=cell[i-1][j] #๋ฐฐ๋‚ญ์— ๋„ฃ์ง€ ๋ชปํ•˜๋ฉด ๊ทธ๋Œ€๋กœ ์ด์ „๊ฐ’
    print(*cell)
    
    knapsack(w,v,3,4)

 

 

4. ๋ถ€๋ถ„๋ฌธ์ž์—ด ๋ฌธ์ œ

์–ด๋– ํ•œ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ํ•ฉ์ด ๊ฐ™๋„๋ก ๋ฌธ์ž์—ด์„ ์„ ํƒํ•  ๋•Œ, ๊ทธ ๊ธธ์ด๊ฐ€ ์ตœ๋Œ€๊ฐ€ ๋˜๋„๋ก ํ•˜๋Š” ๋ฌธ์ œ

(์ฆ‰, [0-3]์˜ ๋ฌธ์ž์—ด์„ ์„ ํƒํ•˜๋ฉด [0-1]๊นŒ์ง€ ๋ฌธ์ž์—ด์˜ ํ•ฉ๊ณผ [2-3]๊นŒ์ง€ ๋ฌธ์ž์—ด์˜ ํ•ฉ์ด ๊ฐ™๊ณ  ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 4)

 

์ด ๋ฌธ์ œ๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ bruteforce ๋ฐฉ์‹์œผ๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‹ค ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‹ค ํƒ์ƒ‰ํ•˜๊ณ  ์ด๊ฐ€ 3์ค‘ for๋ฌธ์œผ๋กœ ๊ตฌํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์— O(n^3)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ง€๋‹Œ๋‹ค.

 

bruteforce ์ฝ”๋“œ ๊ตฌํ˜„

string='9430726'

def maxSubstring(string):
    n=len(string)
    result=0

    for i in range(n):
        for j in range(i+1,n,2):
            leng=j-i+1 #ํ˜„์žฌ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด
            if(result>=leng):
                continue
            lsum=0;rsum=0
            for k in range(leng//2):
                lsum+=int(string[i+k])
                rsum+=int(string[i+k+leng//2])
            if lsum==rsum:
                result=leng
        
    return result

print(maxSubstring(string))

 

๋ฌธ์ œ๋ฅผ ์ž์„ธํžˆ ๋“ค์—ฌ๋‹ค๋ณด๋ฉด, ์ด๋ฏธ ํƒ์ƒ‰ํ–ˆ๋˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด๋“ค์— ๋Œ€ํ•ด์„œ๋Š” 

dp[i][j]=dp[i][k]+dp[k][j] ๋กœ ์ ํ™”์‹์„ ์„ธ์šธ ์ˆ˜ ์žˆ๋‹ค.

์ด๋•Œ k๋Š” i-j์˜ ๊ธธ์ด์˜ ์ ˆ๋ฐ˜์œผ๋กœ ๋‘ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„๋Š” ๊ธฐ์ค€์ด๋ฉฐ 

dp[i][k] ๊ฐ’๊ณผ dp[k+1][j]์˜ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ํ•ฉ์ด ๊ฐ™์œผ๋ฏ€๋กœ ๊ธฐ์กด์˜ result๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.

์ด๋Š” 2์ค‘ for๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— O(n^2)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.

 

dp์ฝ”๋“œ ๊ตฌํ˜„

string='9430726'

def maxString(string):
    result=0
    n=len(string)
    dp=[[0]*n for _ in range(n)]
    for i in range(n):
        dp[i][i]=int(string[i]) #์ž๊ธฐ ์ž์‹  ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” =

    for leng in range(2,n+1):
        for i in range(n-leng+1):
            j=i+leng-1
            k=(i+j)//2
            #j,k
            dp[i][j]=dp[i][k]+dp[k+1][j]
            if dp[i][k]==dp[k+1][j] and leng%2==0:
                result=max(result,leng)
    print(dp)
    return result
print(maxString(string))

 

 

5. ์ตœ์†Œ๋น„์šฉ๊ฒฝ๋กœ

(0,0)์—์„œ (m-1, n-1) ๊นŒ์ง€ ์ด๋™ํ•  ๋•Œ, ์ตœ์†Œ ๋น„์šฉ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ด ๋–„ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ (+1,0) ๋˜๋Š” (0,+1) ๋กœ ๋‘๊ฐ€์ง€ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์ด๋™ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

[x][y]๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ์— [x-1][y]๊นŒ์ง€ ์ด๋™ํ•œ ๋น„์šฉ์— cost[x][y]์™€ [x][y-1]๊นŒ์ง€ ์ด๋™ํ•œ ๋น„์šฉ์— cost[x][y]๋ฅผ ๋”ํ•œ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

์ด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด

m=3
n=4
cost=[[1,3,5,8],[4,2,1,7],[4,3,2,3]]

def minPath(cost,m,n):
    if m==0 and n==0:
        return cost[0][0]
    if m==0:
        return minPath(cost,0,n-1)+cost[0][n]
    if n==0:
        return minPath(cost,m-1,0)+cost[m][0]
    
    x=minPath(cost,m-1,n)
    y=minPath(cost,m,n-1)

    return min(x,y)+cost[m][n]

print(minPath(cost,m-1,n-1))

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋Š” ํ•˜์œ„ ๋ถ€๋ถ„์˜ ๊ณ„์‚ฐ์ด ๋ฐ˜๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ์—

memoization ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๋ฉด

m=3
n=4
cost=[[1,3,5,8],[4,2,1,7],[4,3,2,3]]
memo=[[0]*n for _ in range(m)]

def minPath(cost,m,n):

    if memo[m][n]!=0:
        return memo[m][n]
    ##์ด์ „์— ๊ณ„์‚ฐ๋˜์—ˆ๋‹ค๋ฉด ๋ฐ˜๋ณต ๊ณ„์‚ฐ X ๋ฐ”๋กœ ๊ฒฐ๊ณผ return
    if m==0 and n==0:
        memo[m][n]=cost[0][0]
        
    elif m==0:
        memo[m][n]=minPath(cost,0,n-1)+cost[0][n]
    elif n==0:
        memo[m][n]=minPath(cost,m-1,0)+cost[m][0]
    else:
        x=minPath(cost,m-1,n)
        y=minPath(cost,m,n-1)
        memo[m][n]=min(x,y)+cost[m][n]
        #๋‘˜ ์ค‘ ๋” ์ž‘์€ ๊ฐ’์œผ๋กœ ์„ 
    return memo[m][n]
print(minPath(cost,m-1,n-1))

์œ„์ฒ˜๋Ÿผ memo ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์ด์ „์— ๊ณ„์‚ฐ๋˜์—ˆ๋˜ ๊ฐ’๋“ค์€ ๋”์ด์ƒ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ return ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฅผ ๋ฐ˜๋ณต๋ฌธ๊ณผ ๋‹ค์Œ์˜ ์ ํ™”์‹

dp[x][y]=min(dp[x-1][y],dp[x][y-1])+cost[x][y]

์„ ์‚ฌ์šฉํ•˜์—ฌ DP๋กœ ๊ตฌํ˜„ํ•ด๋ณด๋ฉด

m=3
n=4
cost=[[1,3,5,8],[4,2,1,7],[4,3,2,3]]

dp=[[10**6]*n for _ in range(m)]
def minPath(cost,m,n):
    dp[0][0]=cost[0][0]
    for i in range(1,m):
        dp[i][0]=dp[i-1][0]+cost[i][0]
    for j in range(1,n):
        dp[0][j]=dp[0][j-1]+cost[0][j]
    for x in range(1,m):
        for y in range(1,n):
            dp[x][y]=min(dp[x-1][y],dp[x][y-1])+cost[x][y]

    return dp[m-1][n-1]

print(minPath(cost,m,n))

์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋•Œ, dp[0][0] ๊ฐ’์€ ์ž๊ธฐ ์ž์‹ ์˜ ๋น„์šฉ์ด๋ฏ€๋กœ cost[0][0]์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค.

0๋ฒˆ์งธ ํ–‰์ด๋‚˜ 0๋ฒˆ์งธ ์—ด์˜ ๊ฐ’์€ ์ด๋™์ด ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ์ œํ•œ์ ์ด๋ฏ€๋กœ ๊ทธ์— ๋งž๊ฒŒ ์ ํ™”์‹์„ ์„ธ์›Œ์ค€๋‹ค.

 

 

6. ํƒ€์ผ์ฑ„์šฐ๊ธฐ

๋†’์ด๊ฐ€ 2์ด๊ณ  ๋„ˆ๋น„๊ฐ€ n์ธ ๋ฐ”๋‹ฅ์ด ์žˆ์„ ๋•Œ, ๋†’์ด๊ฐ€ 2์ด๊ณ  ๋„ˆ๋น„๊ฐ€ 1์ธ ํƒ€์ผ์œผ๋กœ ๋ฐ”๋‹ฅ์„ ์ฑ„์šธ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

 

๋„ˆ๋น„๊ฐ€ 1์ธ ๊ฒฝ์šฐ์—๋Š” ํ•œ๊ฐ€์ง€,

๋„ˆ๋น„๊ฐ€ 2์ธ ๊ฒฝ์šฐ์—๋Š” ||,= ์œผ๋กœ ๋‘๊ฐ€์ง€ ์ด๋ฏ€๋กœ ์ด ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•ด๋‘๊ณ 

๊ทธ ๋‹ค์Œ ํƒ€์ผ๋ถ€ํ„ฐ๋Š” i-1 ๋„ˆ๋น„์˜ ๋ฐ”๋‹ฅ์„ ์ฑ„์šฐ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜์™€ i-2 ๋„ˆ๋น„์˜ ๋ฐ”๋‹ฅ์„ ์ฑ„์šฐ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ํ•œ ๊ฐ’๊ณผ ๊ฐ™๋‹ค.

 

์ฆ‰, ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด๊ณผ ๋น„์Šทํ•œ ์ ํ™”์‹ 

dp[i]=dp[i-2]+dp[i-1]

์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ์ด ์‹์—์„œ ์ดˆ๊ธฐํ™” ๊ฐ’ (dp[1],dp[2])๋งŒ ๋‹ค๋ฅธ ๋ฌธ์ œ์ด๋‹ค.

 

DP์ฝ”๋“œ

def tilePlacement(n):
    tiles=[0]*(n+1)
    tiles[1]=1
    tiles[2]=2

    for i in range(3,n+1):
        tiles[i]=tiles[i-1]+tiles[i-2]
    print(tiles)
    return tiles[n]

print(tilePlacement(5))

 

 

7. ํŠน์ • ์ ์ˆ˜์— ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜

ํ•œ ๋ฒˆ์— ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ ์ˆ˜๊ฐ€ 3์ , 5์ , 10์ ์ด๋ผ๊ณ  ํ•  ๋•Œ, ํŠน์ • ์ ์ˆ˜์— ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

score(i-3) + score(i-5) + score(i-10) ๊ฐ’์„ ๋”ํ•˜์—ฌ์„œ socre(i)์˜ ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด

def score(n):
    if n<0:
        return 0
    elif n==0:
        return 1
    else:
        return score(n-3)+score(n-5)+score(n-10)

์œ„์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์žฌ๊ท€ ๋Œ€์‹  ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜์œ„ ๋ฌธ์ œ์˜ ๋ฐ˜๋ณต ๊ณ„์‚ฐ์„ ํ”ผํ•˜๋„๋ก 

def score(n):
    dp=[0]*(n+1)
    dp[0]=1
    for i in range(1,n+1):
        if i-3>=0:
            dp[i]+=dp[i-3]
        if i-5>=0:
            dp[i]+=dp[i-5]
        if i-10>=0:
            dp[i]+=dp[i-10]
    return dp

์ด์ฒ˜๋Ÿผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์žฌ๊ท€์—์„œ๋Š” 0๋ณด๋‹ค ๊ฐ™์€ ๊ฐ’์ด๋‚˜ ์ž‘์€ ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด ๋ฐ”๋กœ 0์ด๋‚˜ 1์„ return ํ•ด์ฃผ์—ˆ๊ณ 

DP์—์„œ๋Š” i๊ฐ’์ด 3,5,10๋ณด๋‹ค ํฐ์ง€ ๋น„๊ตํ•˜์—ฌ ๊ฐ ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด dp[i-3]์„ ๋”ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

 

 

 

8. ์ตœ๋Œ€ ์—ฐ์† ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ

์—ฐ์†๋˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋„๋ก ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ์„ ํƒํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

naiveํ•˜๊ฒŒ ์ ‘๊ทผํ•˜๋ฉด ๋ชจ๋“  ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

n=8
arr=[-2,-3,4,-1,-2,1,5,-3]

def maxsub():
    temp=result=0
    for i in range(n):
        for j in range(i,n):
            temp=0
            for k in range(i,j+1):
                temp+=arr[k]
            result=max(temp,result)
    return result
print(maxsub())

####->๋น„ํšจ์œจ์ ์ธ์ฝ”๋“œ

์ด๋ฅผ ๋” ํšจ์œจ์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ ์œ„์น˜๊นŒ์ง€์˜ ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ์„ ์ •ํ•˜๋Š” arr ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ด ๊ฐ’์ด 0๋ณด๋‹ค ์ž‘์•„์ง€๋ฉด ์ด๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ arr ๋ณ€์ˆ˜๋ฅผ 0 ๊ฐ’์œผ๋กœ ๋ฐ”๊พธ์–ด์ฃผ๋ฉด์„œ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

n-1๊นŒ์ง€ for๋ฌธ์„ ๋Œ๋ฉด์„œ result ๊ฐ’๊ณผ ํ˜„์žฌ์˜ arr ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— for๋ฌธ์„ ๋น ์ ธ๋‚˜์˜จ ๋’ค์˜ result ๊ฐ’์ด ์ตœ๋Œ€ ์—ฐ์† ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ์ด๋‹ค. 

n=8
arr=[-2,-3,4,-1,-2,1,5,-3]

def maxsub():
    curr=result=0

    for i in range(n):
        curr+=arr[i]
        if curr<0:
            curr=0
        result=max(result,curr)
        
    return result

print(maxsub())