Skip to content

Commit ed6b54d

Browse files
committed
programmers.co.kr 코딩테스트 연습_연습문제_카운트 다운
1 parent 6a4fb8b commit ed6b54d

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
3+
def solution(target: int):
4+
bul = [50]
5+
singles = [i for i in range(1, 21)]
6+
bul_singles = bul + singles
7+
bul_singles.sort()
8+
doubles = [i * 2 for i in range(1, 21)]
9+
triples = [i * 3 for i in range(1, 21)]
10+
doubles_triples = doubles + triples
11+
doubles_triples.sort()
12+
13+
14+
dp = [[sys.maxsize, sys.maxsize] for _ in range(0, target + 1)]
15+
dp[0] = [0, 0]
16+
17+
for idx_crnt in range(1, target + 1):
18+
for ib in bul_singles:
19+
idx_prev = idx_crnt - ib
20+
if idx_prev >= 0:
21+
dp[idx_crnt] = my_min([dp[idx_prev][0] + 1, dp[idx_prev][1] + 1], dp[idx_crnt])
22+
else:
23+
break
24+
25+
for id in doubles_triples:
26+
idx_prev = idx_crnt - id
27+
if idx_prev >= 0:
28+
dp[idx_crnt] = my_min([dp[idx_prev][0] + 1, dp[idx_prev][1]], dp[idx_crnt])
29+
else:
30+
break
31+
32+
return dp[target]
33+
34+
def my_min(one, two):
35+
if one[0] < two[0]:
36+
return one
37+
38+
if one[0] > two[0]:
39+
return two
40+
41+
if one[0] == two[0]:
42+
if one[1] > two[1]:
43+
return one
44+
elif one[1] < two[1]:
45+
return two
46+
47+
return one
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from unittest import TestCase
2+
from main import solution
3+
4+
5+
class Test(TestCase):
6+
def test1_solution(self):
7+
self.assertEqual([1, 0], solution(21))
8+
9+
def test2_solution(self):
10+
self.assertEqual([2, 2], solution(58))

0 commit comments

Comments
 (0)