Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 프린터 #171

Closed
hwangJi-dev opened this issue Mar 31, 2023 · 0 comments
Closed

[Algorithm] 프린터 #171

hwangJi-dev opened this issue Mar 31, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Mar 31, 2023

💬 문제

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


💬 Idea

1. 인쇄 대기목록의 가장 앞에 있는 문서(J)를 대기목록에서 꺼냅니다.
2. 나머지 인쇄 대기목록에서 J보다 중요도가 높은 문서가 한 개라도 존재하면 J를 대기목록의 가장 마지막에 넣습니다.
3. 그렇지 않으면 J를 인쇄합니다.

💬 풀이

import Foundation

func solution(_ priorities:[Int], _ location:Int) -> Int {
    var priorities = priorities.reversed().map({ Int($0) })
    var printQueue: [Int] = []
    var idx = abs(location - (priorities.count - 1))
    
    while !priorities.isEmpty {
        let p = priorities.popLast()!
        if p < priorities.max() ?? p {
            idx = idx == priorities.count ? 0 : idx + 1
            priorities.insert(p, at: 0)
        } else {
            printQueue.append(p)
            if idx == priorities.count {
                break
            }
        }
    }
    
    return printQueue.count
}

소요시간 : 30분

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant