Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Last active July 4, 2020 07:23
Show Gist options
  • Save DongguemYoo/11ec0179facc16a97dccfbe1bc7bf453 to your computer and use it in GitHub Desktop.
Save DongguemYoo/11ec0179facc16a97dccfbe1bc7bf453 to your computer and use it in GitHub Desktop.
[코드잇] 새콤달콤 장사 tabulation
배운점
max 값만 추출하는 방법
profit = max(profit, profit_table[j] + profit_table[i - j])
가능한 조합 만드는 for문
for j in range(1, i // 2 + 1):
// 은 좌항 나누기 우항인데 정수형이고
/ 은 좌항 나누기 우항인데 실수형이다
def max_profit(price_list, count):
# 개수별로 가능한 최대 수익을 저장하는 리스트
# 새꼼달꼼 0개면 0원
profit_table = [0]
# 개수 1부터 count까지 계산하기 위해 반복문
for i in range(1, count + 1):
# profit은 count개를 팔아서 가능한 최대 수익을 저장하는 변수
# 팔려고 하는 총개수에 대한 가격이 price_list에 있으면 일단 그 가격으로 설정
# 팔려고 하는 총개수에 대한 가격이 price_list에 없으면 일단 0으로 설정
if i < len(price_list):
profit = price_list[i]
else:
profit = 0
# count개를 팔 수 있는 조합들을 비교해서, 가능한 최대 수익을 찾는다
for j in range(1, i // 2 + 1):
profit = max(profit, profit_table[j] + profit_table[i - j])
profit_table.append(profit)
return profit_table[count]
# 테스트
print(max_profit([0, 200, 600, 900, 1200, 2000], 5))
print(max_profit([0, 300, 600, 700, 1100, 1400], 8))
print(max_profit([0, 100, 200, 400, 600, 900, 1200, 1300, 1500, 1800], 9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment