You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
그 다음 picks가 모두 0이 되어 곡괭이가 소진되는 경우, sumArrByStoneTools를 모두 다 돈 경우가 아닐 때까지 반복하며 정답을 구한다.
다이아몬드 곡괭이가 남아있다면, 다이아몬드 곡괭이를 우선적으로 소진한다. (다이아 → 철 → 돌 순)
ifpicks[0]>0{picks[0]-=1
sum +=calSum(1,sumArrByStoneTools[idx].0)}elseifpicks[1]>0{picks[1]-=1
sum +=calSum(2,sumArrByStoneTools[idx].0)}else{picks[2]-=1
sum +=calSum(3,sumArrByStoneTools[idx].0)}
<합계 구하는 메서드>
func calSum(_ type:Int, _ minerals:[String])->Int{lettoolDict:[Int:[Int]]=[1:[1,1,1],2:[5,1,1],3:[25,5,1]]varsum=0foriin minerals {if i =="diamond"{
sum +=toolDict[type]![0]}elseif i =="iron"{
sum +=toolDict[type]![1]}else{
sum +=toolDict[type]![2]}}return sum
}
💬 풀이
import Foundation
func solution(_ picks:[Int], _ minerals:[String])->Int{varsumArrByStoneTools:[([String],Int)]=[]letmaxToolCount= picks.reduce(0,+)foriinstride(from:0, to: minerals.count, by:5){vargijoon=0if i +4< minerals.count {
gijoon = i +4}else{
gijoon = minerals.count -1}letm=minerals[i...gijoon].map({String($0)})vartemp=0forjin m {if j =="diamond"{
temp +=25}elseif j =="iron"{
temp +=5}else{
temp +=1}}
sumArrByStoneTools.append((m, temp))}if sumArrByStoneTools.count > maxToolCount {
sumArrByStoneTools =Array(sumArrByStoneTools[0..<maxToolCount])}
sumArrByStoneTools = sumArrByStoneTools.sorted(by:{ $0.1> $1.1})varsum=0varidx=0varpicks= picks
while picks.allSatisfy({ $0 ==0})==false && idx < sumArrByStoneTools.count {ifpicks[0]>0{picks[0]-=1
sum +=calSum(1,sumArrByStoneTools[idx].0)}elseifpicks[1]>0{picks[1]-=1
sum +=calSum(2,sumArrByStoneTools[idx].0)}else{picks[2]-=1
sum +=calSum(3,sumArrByStoneTools[idx].0)}
idx +=1}return sum
}func calSum(_ type:Int, _ minerals:[String])->Int{lettoolDict:[Int:[Int]]=[1:[1,1,1],2:[5,1,1],3:[25,5,1]]varsum=0foriin minerals {if i =="diamond"{
sum +=toolDict[type]![0]}elseif i =="iron"{
sum +=toolDict[type]![1]}else{
sum +=toolDict[type]![2]}}return sum
}
The text was updated successfully, but these errors were encountered:
💬 문제
https://school.programmers.co.kr/learn/courses/30/lessons/172927
💬 Idea
<그리디>
곡괭이를 하나 선택해서 광물 5개를 연속으로 캐기 위해서 for문을 5씩 기준으로 돈다.
sumArrByStoneTools
배열에 저장한다.이후 곡괭이가 먼저 소진되는 경우 해당 곡괭이 수에 비례하는 광물 개수만 캐기 위해 해당 작업을 처리해주고,
우선순위가 높은 순서대로
sumArrByStoneTools
배열을 정렬한다. →그 다음 picks가 모두 0이 되어 곡괭이가 소진되는 경우, sumArrByStoneTools를 모두 다 돈 경우가 아닐 때까지 반복하며 정답을 구한다.
<합계 구하는 메서드>
💬 풀이
The text was updated successfully, but these errors were encountered: