TIL #54

loci·2024년 6월 23일
0

TIL

목록 보기
52/103


기능 개발

각 진행과정을 나타내는 배열이 주어지고 각 진행과정이 몇 퍼센트식 진행되는지 speeds 배열이 같이 주어진다. progresses의 진행과정들이 100이되었을때 배포를 하게 되는데 하루에 한번씩 배포가 이루어지고 이 배포가 이루어질때 100이 되면 배포가 되고 또한 이전 진행과정이 100이아니라면 현재진행과정 또한 배포가 되지 못하고 이전 진행과정이 배포가 될때 함께 나가게 된다.
배포가 이루어질때 한번에 몇 개의 진행과정이 배포되는지 배열로 담아 반환 해야한다.

스택이나 큐를 이용한 방식 중에 큐를 이용해서 앞에서부터 100이 되면 나가고 카운트해주고 다음 요소도 100이상이면 나가도록 반복하고 100이하가 나오면 바로 중지되고 다음 연산이 진행되도록 풀었다.


나의 풀이

class Solution {
    fun solution(progresses: IntArray, speeds: IntArray): IntArray {
        var answer = intArrayOf()
        var list = progresses.toMutableList()
        var speed = speeds.toMutableList()
        
       while(list.isNotEmpty()){
        var count = 0
        
        for(i in list.indices){
            list[i] = list[i] + speed[i]
        }
        
        for(i in list.indices){
            if(list[0] >= 100){
                list.removeAt(0)
                speed.removeAt(0)
                count++
            } else {
                break
            }
        }
        
        if(count != 0){
            answer += count            
        }
       }
        return answer
    }
}

처음에 테스트케이스에서 거의 틀리게 나왔는데 speed의 요소도 같이 삭제를 시켜주지 않아 틀리는 문제였다. 그래서 speed.removeAt(0)를 추가해 해결되었다.


다른사람의 코드

class Solution {
   fun solution(progresses: IntArray, speeds: IntArray): IntArray {
        val answer : MutableList<Int> = arrayListOf()
        val days = progresses

        for(i in 0 until progresses.size){
            days[i] = ((100 - progresses[i])/speeds[i])
        }

        var max = days[0]
        var releaseCount = 1

        for(i in 1 until days.size){
            if(max < days[i]){
                answer.add(releaseCount)
                releaseCount = 1
                max = days[i]
            }else{
                releaseCount ++
            }
        }

        answer.add(releaseCount)

        return answer.toIntArray()
    }
}

각 진행과정이 완료까지 며칠이 걸리는지 미리 계산해서 풀어주는 방식

profile
편리한 개발자

0개의 댓글