Get Up & Code, MacKin Talk

[백준]1966_프린터큐 본문

IOS/알고리즘 문제 풀이

[백준]1966_프린터큐

맥킨 2021. 10. 11. 19:02

목차



문항 분석

입력 제한
큐 자료구조를 활용해야 하는 문제

Swift에서 Array를 활용해 큐를 간단하게 구현하게 될 경우

  • removeFirst() method : O(N)
  • append() method: O(1)

순서에 맞는 자료 값에 대한 위치 계산을 계속 진행해야 하고,
반복문 안에서 점검을 진행해줘야 한다.

Code

import Foundation

let testcase = Int(readLine()!)!

var list = [(Int, Int)]()

for _ in 0..<testcase {
    var result = 0
    let inputs = readLine()!.split(separator: " ").map { Int(String($0))!}
    var currentLocation = inputs[1]
    var values = readLine()!.split(separator: " ").map { Int(String($0))!}
    let targetValue = values[currentLocation]

    while true {
        // 값이 하나도 없을 경우.
        if values.isEmpty { break }
        // 해당 값이 원하는 값일 경우.
        if values.first == values.max() {
            result += 1
            // 첫 번째 값이 최댓 값인 경우 종료
            if currentLocation == 0 {
                break
            }
            values.removeFirst()
            currentLocation -= 1
        } else {
            values.append(values.removeFirst())
            currentLocation = currentLocation == 0 ? values.endIndex-1 : currentLocation - 1
        }
    }
    print(result)
    list.removeAll()    
}

결과 및 분석

시간 복잡도를 크게 고려하지 않고 문제에 대한 해결을 진행했다.
입력으로 들어오는 N의 개수 제한이 있었던 점 덕분에 시간초과에 대한 걱정은 없었던 문제
크게 도움이 되는지는 모르겠으나, 큐 자료구조를 사용할 수 있었던 문항이다.

'IOS > 알고리즘 문제 풀이' 카테고리의 다른 글

[백준]15652 N과 M(4)  (0) 2021.10.19
[백준]1463_1로 만들기  (0) 2021.10.16
[백준]11047_동전0  (0) 2021.10.06
[백준]18830_좌표압축  (0) 2021.10.06
[백준]5430_AC  (0) 2021.10.01