본문 바로가기
프로그래머스/코딩연습1

[프로그래머스] 프린터 (42587)(Kotlin)

by 유줘니 2020. 1. 13.

원본 문제 : https://programmers.co.kr/learn/courses/30/lessons/42587?language=kotlin

 

 

<첫번째>

import java.util.*

data class Doc (val priority: Int, val idx: Int)

class Solution {
    fun solution(priorities: IntArray, location: Int): Int {
        var answer = 1
        val printList: LinkedList<Doc> = LinkedList<Doc>()

        for ( i in priorities.indices)
            printList.add(Doc(priorities[i], i))

        while(printList.size > 1) {
            val firstDoc: Doc = printList.peek()

            for( i in 1 until printList.size) {
                if(firstDoc.priority < printList[i].priority) {
                    printList.add(printList.poll())
                    break
                }

                if(i == printList.size - 1) {
                    if(firstDoc.idx == location) return answer
                    printList.poll()
                    answer++
                }

            }
        }

        return answer
    }
}

 

<두번째> 2020/06/05

import java.util.*

data class Doc(val p: Int, val i: Int)

fun solution(priorities: IntArray, location: Int): Int {

    val list: LinkedList<Doc> = LinkedList<Doc>()
    priorities
        .forEachIndexed { index, value ->
        	list.add(Doc(value, index))
        }

    var answer: Int = 0

    while (true) {

        val now = list.poll()

        if (list.any { now.p < it.p }) {
            list.add(now)
        } else {
            answer++
            if (now.i == location)
                break
        }

    }

    return answer

}

댓글