fun solution(priorities: IntArray, location: Int): Int {
var printQueue = priorities.withIndex().toMutableList()
var count = 0
while (printQueue.isNotEmpty()) {
val current: IndexedValue<Int> = printQueue.first()
printQueue.removeAt(0)
if(printQueue.any { current.value < it.value }) {
printQueue.add(current)
} else {
count += 1
if (location == current.index) {
break
}
}
}
return count
}
처음에 생각을 어렵게 했다가, 가장 큰거부터 오른쪽으로 원형 배열처럼 계속 돌면서 지워나가면 되겠다고 생각했다.
근데 그렇게 생각을 했어도 큐가 생각이 안났다 ㅠㅠ
원형 배열을 만드느라고 인덱스 가지고 고생하다가, 결국 큐를 써야한다는 걸 알고 코드를 참고했다..
withIndex().toMutableList()
를 통해 인덱스와 함께 리스트를 만들어야한다. Queue를 써도 되지만 List를 통해서도 가능하기 때문any
를 사용하는데, '1개라도 조건에 맞는게 있다면' 이다. 1개라도 현재값보다 큰게 있다면 다시 뒤에다가 addIndexedValue
라 가능)이 location 이랑 같다면 끝내고 count를 반환한다.