Skip to content

Commit 24214fd

Browse files
committed
solved(python): programmers
- PCCP 기출문제 / 광물 캐기
1 parent 86aca39 commit 24214fd

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

programmers/연습문제/python/광물_캐기/__init__.py

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import math
2+
3+
from collections import deque
4+
5+
6+
def solution(picks, minerals):
7+
n = len(minerals)
8+
stack, minimum = deque([(0, 0, picks)]), math.inf
9+
10+
mineral_cost = {"diamond": 25, "iron": 5, "stone": 1}
11+
while stack:
12+
idx, cost, remains = stack.pop()
13+
if cost > minimum:
14+
continue
15+
16+
if idx == (n // 5 + int(n % 5 != 0)) or sum(remains) == 0:
17+
minimum = min(minimum, cost)
18+
continue
19+
20+
for pick, remain in enumerate(remains):
21+
if remain == 0:
22+
continue
23+
24+
new_cost = 0
25+
for mineral in minerals[idx * 5 : min((idx + 1) * 5, n)]:
26+
new_cost += max(mineral_cost[mineral] // 5 ** (2 - pick), 1)
27+
28+
new_remains = remains[:]
29+
new_remains[pick] -= 1
30+
31+
stack.append((idx + 1, cost + new_cost, new_remains))
32+
33+
return minimum
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[
2+
{
3+
"params": [
4+
[
5+
1,
6+
3,
7+
2
8+
],
9+
[
10+
"diamond",
11+
"diamond",
12+
"diamond",
13+
"iron",
14+
"iron",
15+
"diamond",
16+
"iron",
17+
"stone"
18+
]
19+
],
20+
"expected": 12
21+
},
22+
{
23+
"params": [
24+
[
25+
0,
26+
1,
27+
1
28+
],
29+
[
30+
"diamond",
31+
"diamond",
32+
"diamond",
33+
"diamond",
34+
"diamond",
35+
"iron",
36+
"iron",
37+
"iron",
38+
"iron",
39+
"iron",
40+
"diamond"
41+
]
42+
],
43+
"expected": 50
44+
}
45+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import os
3+
import unittest
4+
5+
from parameterized import parameterized
6+
7+
from .main import solution
8+
9+
10+
def load_sample(filename: str):
11+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
12+
13+
with open(path, "r") as file:
14+
return [(case["params"], case["expected"]) for case in json.load(file)]
15+
16+
17+
class TestCase(unittest.TestCase):
18+
@parameterized.expand(load_sample("sample.json"))
19+
def test_case(self, params: list, expected: any):
20+
# When
21+
result = solution(*params)
22+
23+
# Then
24+
self.assertEqual(expected, result)
25+
26+
27+
if __name__ == "__main__":
28+
unittest.main()

0 commit comments

Comments
 (0)