TIL #55

loci·2024년 6월 24일
0

TIL

목록 보기
53/103

프로세스

queue를 이용해 location의 해당하는 Index가 몇번째에 실행되는 지 구해야한다. 실행방식은 priorities에서 첫번째 요소가 가장 크면 실행되고 아니면 맨 뒤로 보내는 작업을 반복해준다.

처음엔 location 앞에 최대값이 있으면 Index -1을 리턴하고 아닌면 size+Index-1을 리턴하는 식으로 잘못풀어서 다른사람의 코드를 참고했다.


import java.util.LinkedList

class Solution {
    fun solution(priorities: IntArray, location: Int): Int {
        var answer = 0
        val queue = LinkedList<Int>()
        priorities.sortedDescending().forEach{
            queue.add(it)
        }
        while(!queue.isEmpty()){
            for(i in priorities.indices){
                if(priorities[i] == queue.peek()){
                    queue.poll()
                    answer++
                    if(i == location){
                        return answer
                    }
                }
            }
        }
        
        return answer
    }
}

priorities를 내림차순으로 정렬해 큰수부터 나오도록해 queue에 넣어주고
queue에서 priorities를 순서대로 비교해 같은수가나오면 queue에서 빼준후 answer를 카운트해주고 만약 해당인덱스가 location과 같다면 answer를 리턴해준다.

profile
편리한 개발자

0개의 댓글