입출력 예시
picks | minerals | result |
[1, 3, 2] | ["diamond", "diamond", "diamond", "iron", "iron", "diamond", "iron", "stone"] | 12 |
[0, 1, 1] | ["diamond", "diamond", "diamond", "diamond", "diamond", "iron", "iron", "iron", "iron", "iron", "diamond"] | 50 |
나의 코드
def cal_score(pick, mineral):
'''
mineral: ['diamond', 'iron', 'stone']
'''
if len(mineral) == 0:
return 0
my_dict = {'diamond': 0, 'iron': 1, 'stone': 2} # 곡괭이와 광물을 index로 바꾸기위한 dictionary
fatigue_table = [[1, 1, 1], [5, 1, 1], [25, 5, 1]] # fatigue 점수
fatigue = 0
for m in mineral:
fatigue += fatigue_table[my_dict[pick]][my_dict[m]]
return fatigue
def solution(picks, minerals):
minerals = minerals[:sum(picks)*5] # 캘 수 있는 양만 남기기 * 중요!
batch = [minerals[5*i:5*(i+1)] for i in range(len(minerals)//5+1)] # 5개씩 자르기
batch.sort(key=lambda x: (-x.count('diamond'), -x.count('iron'), -x.count('stone'))) # diamond, iron, stone 갯수 순으로 sort
temp = ['diamond', 'iron', 'stone']
my_picks = []
for i in range(3):
my_picks.extend([temp[i]]*picks[i]) # 곡괭이를 나열
fatigue = 0
for mineral in batch:
if len(my_picks) == 0:
break
pick = my_picks.pop(0) # 곡괭이 사용
fatigue += cal_score(pick, mineral)
return fatigue
Python
복사
다른 풀이
def solution(picks, minerals):
def solve(picks, minerals, fatigue):
if sum(picks) == 0 or len(minerals) == 0:
return fatigue
result = [float('inf')]
for i, fatigues in enumerate(({"diamond": 1, "iron": 1, "stone": 1},
{"diamond": 5, "iron": 1, "stone": 1},
{"diamond": 25, "iron": 5, "stone": 1},)):
if picks[i] > 0:
temp_picks = picks.copy()
temp_picks[i] -= 1
result.append(
solve(temp_picks, minerals[5:], fatigue + sum(fatigues[mineral] for mineral in minerals[:5])))
return min(result)
return solve(picks, minerals, 0)
Python
복사