STUDYING/Algorithm

[Programmers] 프린터

EOZIN 2021. 9. 27. 00:40
728x90

https://programmers.co.kr/learn/courses/30/lessons/42587

import Foundation

func solution(_ priorities:[Int], _ location:Int) -> Int {
    
    var pq: [(Int, Int)] = []
    var res = 0
    
    for i in 0..<priorities.count {
        pq.append((priorities[i], i))
    }
    
    var cnt = 0
    
    while !pq.isEmpty {
        let pqFirstPriority = (pq.first?.0)!
        var isBig = true
        
        for i in 0..<pq.count {
            if pqFirstPriority < pq[i].0 {
                isBig = false
            }
        }
        
        if !isBig {
            let pqFirst = pq.first!
            pq.removeFirst()
            pq.append(pqFirst)
        } else {
            cnt += 1
            if pq.first?.1 == location {
                break;
            }
            pq.removeFirst()
        }
    }
    return cnt
}