문제
func solution(_ priorities:[Int], _ location:Int) -> Int {
var result: [Int] = []
var currentIndex = 0
var arr = priorities.sorted(by: >)
while result.count < priorities.count {
if currentIndex >= priorities.count {
currentIndex = 0
}
let current = priorities[currentIndex]
let max = arr.first ?? 0
if current >= max {
result.append(current)
arr.removeFirst()
if currentIndex == location {
return result.endIndex
}
currentIndex += 1
} else {
currentIndex += 1
}
}
return 0
}

priorities 배열을 그대로 순회하면서 우선순위가 제일 높은지를 비교하고 실행하지만,
"우선순위가 낮으면 큐의 뒤로 보내야 하는 로직"
이 정상 작동하지 않음.
즉,priorities를 회전시키지 않고currentIndex만 증가시키고 있기 때문에 큐의 구조(FIFO)를 반영하지 않은 채 단순히 리스트를 한 바퀴 도는 방식으로 처리되고 있음.
(index, priority) 형태로 저장하고, 우선순위가 더 높은게 뒤에 있다면 enqueue한다.dequeue한다.func solution(_ priorities:[Int], _ location:Int) -> Int {
var queue: [(index: Int, priority: Int)] = priorities.enumerated().map { ($0.offset, $0.element) }
var count = 0
while !queue.isEmpty {
let first = queue.removeFirst()
if queue.contains(where: { $0.priority > first.priority }) {
// 뒤에 더 높은 우선순위가 있으면 다시 뒤로 보냄
queue.append(first)
} else {
// 실행됨
count += 1
if first.index == location {
return count
}
}
}
return count
}
enumerated()를 통해 (index, priority) 튜플로 초기 큐 생성removeFirst())append)count += 1)location에 해당하면 실행 순서를 리턴