쥐수의 공부노트

백준 1021번 회전하는 큐 본문

swift 알고리즘/큐, 덱

백준 1021번 회전하는 큐

쥐수 2023. 6. 16. 14:14
728x90

정답 :

var input = readLine()!.split(separator: " ").map{Int($0)!}
var number = readLine()!.split(separator: " ").map{Int($0)!}
let n = input[0]
let m = input[1]
var array = [Int](1...n)
var result = 0

while !number.isEmpty {
    if array.first! == number.first! {
        array.removeFirst()
        number.removeFirst()
        continue
    }
    let startDistance = array.firstIndex(of: number.first!)!
    let endDistance = array.count - array.firstIndex(of: number.first!)!
    
    if startDistance < endDistance {
        array.append(array.removeFirst())
    } else {
        array.insert(array.removeLast(), at: 0)
    }
    result += 1
    
}
print(result)
더보기

TMI : 난이도가 매우 어렵다.. 문제 이해하는데 좀 오래걸렸다..

728x90

'swift 알고리즘 > 큐, 덱' 카테고리의 다른 글

백준 5430번 AC  (0) 2023.06.16
백준 10866번 덱  (0) 2023.06.14
백준 11866번 요세푸스 문제 0  (0) 2023.06.13
백준 2164번 카드2  (1) 2023.06.13
백준 18258번 큐 2  (0) 2023.06.12