코딩 테스트 연습 - 기능 개발

다용도리모콘·2020년 6월 28일
0

CodingTest

목록 보기
18/34

01. 이해

현재 작업별 진행도 배열과 작업별 진행속도 배열을 입력 받아 각 배포 때 
몇개씩 배포가 진행되는지 반환. 단 앞의 작업이 완료되 지 않으면 뒤의 작업은
완료되더라도 배포될 수 없음

02. 계획

작업 배열을 계속 돌면서 진행 속도를 더하다가 맨 앞의 작업이 100이 되었을 때
앞쪽 부터 100이 넘은 작업을 빼내고 그 갯수를 결과 배열에 넣기. 

03. 실행

fun solution(progresses: IntArray, speeds: IntArray): IntArray {
    val answer = ArrayList<Int>()
    var progressQueue: Queue<Int> = LinkedList<Int>(progresses.toList())
    val speedQueue: Queue<Int> = LinkedList<Int>(speeds.toList())

    while (progressQueue.isNotEmpty()) {
        var count = 0

        if (progressQueue.size == 1) {
            answer.add(1)
            break
        }

        val currentSpeedQueue = speedQueue.toList()
        progressQueue = LinkedList<Int>(progressQueue.mapIndexed
        { index, i -> i + currentSpeedQueue[index] })

        while (progressQueue.isNotEmpty()) {

            if (progressQueue.peek()!! >= 100) {
                progressQueue.poll()
                speedQueue.poll()
                count += 1
            } else {
                break
            }

        }

        if (count != 0)
            answer.add(count)

    }

    return answer.toIntArray()
}

04. 회고

queue로 문제를 풀어 봤는데 코틀린에서의 queue 사용법에 대한 정보가 생각보다 적었다.
어떻게 풀지 계획을 세우는 것 까지는 쉬웠는데 코드로 풀어내기가 어려웠다.
코틀린을 일할 때 사용하지 않고 코테 문제 풀때만 사용하니까 점점 더 헤메는 느낌...

0개의 댓글