Notice
Recent Posts
Recent Comments
Today
Total
01-26 01:17
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[์šฐ์„ ์ˆœ์œ„ํ] ํ”„๋ฆฐํ„ฐ (์ž๋ฐ”) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[์šฐ์„ ์ˆœ์œ„ํ] ํ”„๋ฆฐํ„ฐ (์ž๋ฐ”)

WonderJay 2022. 9. 4. 00:05
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://school.programmers.co.kr/learn/courses/30/lessons/42587/solution_groups?language=java&type=my 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

  ๋ฌธ์„œ์˜ ์šฐ์„ ์ˆœ์œ„(์ค‘์š”๋„)๊ฐ€ ๋‹ด๊ธด priorities ๋ฐฐ์—ด๊ณผ, ํŠน์ • ๋ฌธ์„œ์˜ ์œ„์น˜ location ์ด ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ location ์— ์œ„์น˜ํ•œ ๋ฌธ์„œ๋Š” ์‹ค์ œ๋กœ ๋ช‡ ๋ฒˆ์งธ๋กœ ์ถœ๋ ฅ์ด ๋˜๋Š”์ง€ answer ์— ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์šฐ์„ ์ˆœ์œ„ํ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ˆซ์ž๊ฐ€ ๋†’์„ ์ˆ˜๋ก ์ค‘์š”๋„๊ฐ€ ๋†’๋‹ค๊ณ  ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•˜์˜€์œผ๋ฏ€๋กœ, PriorityQueue ์— Collections.reverseOrder() ์„ ์ „๋‹ฌํ•˜์—ฌ ์ค‘์š”๋„๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ poll ์ด ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. 

 

  ๊ทธ๋ฆฌ๊ณ  priorities ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ, ํ˜„์žฌ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅ๋œ ๊ฐ€์žฅ ๋†’์€ ์ค‘์š”๋„ == priorities[i] ๋ผ๋ฉด ํ•ด๋‹น ์œ„์น˜(i) ๊ฐ€ location ์ธ์ง€ ํ™•์ธํ•˜๊ณ  ๋งž๋‹ค๋ฉด answer ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ•ด๋‹น ์œ„์น˜(i) ๊ฐ€ location ์ด ์•„๋‹ˆ๋ผ๋ฉด answer ์„ ํ•˜๋‚˜ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๊ณ  pop/poll ํ•ด์ค€๋‹ค. location ์— ์œ„์น˜ํ•œ ๋ฌธ์„œ์—๋งŒ ๊ด€์‹ฌ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

import java.io.IOException;
import java.util.Collections;
import java.util.PriorityQueue;

class Solution {
    public int solution(int[] priorities, int location) throws IOException {
        int answer = 1;
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        for(int ele : priorities)
            pq.add(ele);

        while(!pq.isEmpty()){
            for(int i = 0 ; i < priorities.length; i++){
                if(pq.peek() == priorities[i]){
                    if(i == location) return answer;
                    else {
                        answer++;
                        pq.poll();
                    }
                }
            }
        }
        return answer;
    }
}
728x90
๋ฐ˜์‘ํ˜•
LIST
Comments