Algorithm/프로그래머스
프로그래머스 : 프린터(Swift)
콩벌레 개발자
2021. 9. 15. 21:29
큐에 해당하는 문제지만 딱히 큐를 쓰지 않아도 되는 문제. 풀이도 어떻게 보면 큐지만 큐가 아닌것. 이번에도 커서를 이용해 풀이했다.
stayList에 현재 커서의 위치보다 작은 값의 유무를 Bool로 받는 배열을 생성하고 그 배열에 false가 없을 시 커서의 위치를 +1을 한다. 배열에 false가 있을 시 출력해야 하는 위치를 변경한다.
import Foundation
func solution(_ priorities:[Int], _ location:Int) -> Int {
var nowLocation = location
var myQueue : [Int] = []
var count = 1
for item in priorities {
myQueue.append(item)
}
var cursor = 0
while cursor <= nowLocation {
var stayList = myQueue[(cursor+1)..<myQueue.count].map({myQueue[cursor] >= $0})
if stayList.contains(false) {
myQueue.append(myQueue[cursor])
myQueue.remove(at: cursor)
if cursor == nowLocation {
nowLocation = myQueue.count-1
}else{
nowLocation -= 1
}
}else{
if cursor == nowLocation {
break
}
cursor += 1
count += 1
}
}
return count
}