알고리즘/프로그래머스
[Kotlin] 다리를 지나는 트럭
hongeeii
2024. 1. 17. 17:50
728x90
반응형
import java.util.*
class Solution {
fun solution(bridge_length: Int, weight: Int, truck_weights: IntArray): Int {
var answer = 0
var totalWeight = 0 // 다리 위에 있는 트럭의 무게
val truckQueue = LinkedList<Int>(truck_weights.toList())
val bridgeQueue = LinkedList<Int>(IntArray(bridge_length) { 0 }.toList())
while (true) {
if (truckQueue.isEmpty() && totalWeight == 0) break
totalWeight -= bridgeQueue.pop()
if (truckQueue.isNotEmpty() && totalWeight + truckQueue.peek() <= weight) {
val truck = truckQueue.pop()
bridgeQueue.add(truck)
totalWeight += truck
} else {
bridgeQueue.add(0)
}
answer++
}
return answer
}
}
728x90
반응형