-
백준(BaekJoon)_11052_카드구매하기Algorithm/Problems_Solving 2022. 11. 11. 09:24
cardNum = int(input()) profit_table = [0]*(cardNum+1) cardPrice = [0]+list(map(int, input().split())) # print(cardPrice) def solution(): # profit 0개는 항상 0... 1개는 price의 1개와 같기 떄문에 아래처럼 # 2개부터 경우의 수가 생긴다 profit_table[0], profit_table[1] = 0, cardPrice[1] for i in range(2, cardNum+1): for j in range(1, i+1): # j는 항상 i보다 작거나 같다. 따라서 i-j보다 작거나 같다. # profit_table보다 card_price으 값이 먼저 있어야 profit이 계산이 되기 때문에 card_price의 j(작은값)을 주고 profit_table[i] = max(profit_table[i], profit_table[i-j] + cardPrice[j]) return profit_table[cardNum] print(solution())
profit_table[i] = max(profit_table[i], profit_table[i-j] + cardPrice[j]) 설명..
profit_table[2] = profit_table[1] + card_price[0] profit_table[3] = profit_table[2] + card_price[1] profit_table[1] + card_price[2] profit_table[0] + card_price[3]
'Algorithm > Problems_Solving' 카테고리의 다른 글
백준(BaekJoon) 2606 - 바이러스 (0) 2022.11.27 프로그래머스(Programmers) LEVEL1 - 같은숫자는싫어 (0) 2022.11.15 백준(Baekjoon) 9095 - 1,2,3 더하기 (0) 2022.10.31 백준(BaekJoon) 11407 - 동전 (0) 2022.10.22 프로그래머스(Programmers) LEVEL1 - 소수만들기 (0) 2022.10.13