Contents

ํ”„๋กœ์„ธ์Šค(with. Java)

   Sep 24, 2024     3 min read

ํ”„๋กœ์„ธ์Šค(with. Java)์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณธ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ, ํ’€์—ˆ๋˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํšŒ๊ณ ์™€ ๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋ฉฐ, ์•Œ์•„๊ฐ€๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ์— ๋Œ€ํ•ด ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ

์šด์˜์ฒด์ œ์˜ ์—ญํ•  ์ค‘ ํ•˜๋‚˜๋Š” ์ปดํ“จํ„ฐ ์‹œ์Šคํ…œ์˜ ์ž์›์„ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ์—์„œ๋Š” ์šด์˜์ฒด์ œ๊ฐ€ ๋‹ค์Œ ๊ทœ์น™์— ๋”ฐ๋ผ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ด€๋ฆฌํ•  ๊ฒฝ์šฐ ํŠน์ • ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ช‡ ๋ฒˆ์งธ๋กœ ์‹คํ–‰๋˜๋Š”์ง€ ์•Œ์•„๋‚ด๋ฉด ๋ฉ๋‹ˆ๋‹ค.

  1. ์‹คํ–‰ ๋Œ€๊ธฐ ํ(Queue)์—์„œ ๋Œ€๊ธฐ์ค‘์ธ ํ”„๋กœ์„ธ์Šค ํ•˜๋‚˜๋ฅผ ๊บผ๋ƒ…๋‹ˆ๋‹ค.
  2. ํ์— ๋Œ€๊ธฐ์ค‘์ธ ํ”„๋กœ์„ธ์Šค ์ค‘ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋” ๋†’์€ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ฐฉ๊ธˆ ๊บผ๋‚ธ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‹ค์‹œ ํ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
  3. ๋งŒ์•ฝ ๊ทธ๋Ÿฐ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์—†๋‹ค๋ฉด ๋ฐฉ๊ธˆ ๊บผ๋‚ธ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. 3.1 ํ•œ ๋ฒˆ ์‹คํ–‰ํ•œ ํ”„๋กœ์„ธ์Šค๋Š” ๋‹ค์‹œ ํ์— ๋„ฃ์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ”„๋กœ์„ธ์Šค 4๊ฐœ [A, B, C, D]๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰ ๋Œ€๊ธฐ ํ์— ๋“ค์–ด์žˆ๊ณ , ์šฐ์„ ์ˆœ์œ„๊ฐ€ [2, 1, 3, 2]๋ผ๋ฉด [C, D, A, B] ์ˆœ์œผ๋กœ ์‹คํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

ํ˜„์žฌ ์‹คํ–‰ ๋Œ€๊ธฐ ํ(Queue)์— ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ค‘์š”๋„๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธด ๋ฐฐ์—ด priorities์™€, ๋ช‡ ๋ฒˆ์งธ๋กœ ์‹คํ–‰๋˜๋Š”์ง€ ์•Œ๊ณ ์‹ถ์€ ํ”„๋กœ์„ธ์Šค์˜ ์œ„์น˜๋ฅผ ์•Œ๋ ค์ฃผ๋Š” location์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ช‡ ๋ฒˆ์งธ๋กœ ์‹คํ–‰๋˜๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • priorities์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • priorities์˜ ์›์†Œ๋Š” 1 ์ด์ƒ 9 ์ดํ•˜์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
  • priorities์˜ ์›์†Œ๋Š” ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ ์ˆซ์ž๊ฐ€ ํด ์ˆ˜๋ก ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์Šต๋‹ˆ๋‹ค.
  • location์€ 0 ์ด์ƒ (๋Œ€๊ธฐ ํ์— ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค ์ˆ˜ - 1) ์ดํ•˜์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  • priorities์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ์œผ๋ฉด 0, ๋‘ ๋ฒˆ์งธ์— ์žˆ์œผ๋ฉด 1 โ€ฆ ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

prioritieslocationreturn
[2, 1, 3, 2]21
[1, 1, 9, 1, 1, 1]05

๋ฌธ์ œ ํ’€์ด

import java.util.Queue;
import java.util.LinkedList;
import java.util.Collections;
import java.util.Arrays;
class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        Queue<Integer> queue = new LinkedList<>();
        int want_num = priorities[location];
        int count = 0;
        for(int i : priorities){
            queue.offer(i);
            if(want_num <= i){
                count++;
            }
        }
        if(count == 0){
            return 1;
        }
        Integer[] arr = Arrays.stream(priorities).boxed().toArray(Integer[]::new);
        Arrays.sort(arr, Collections.reverseOrder());
        int idx = 0;
        while(!queue.isEmpty()){
            int temp = 0;
            if(queue.peek() == arr[idx]){
                answer++;
                idx++;
                queue.poll();
                if(location == 0){
                    break;
                }
                location--;
            }else{
                temp = queue.poll();
                queue.offer(temp);
                location--;
                if(location < 0){
                    location = queue.size() - 1;
                }
            }
        }

        return answer;
    }
}

ํ’€์ด ์„ค๋ช…

์ด ์ฝ”๋“œ๋Š” ํ”„๋ฆฐํ„ฐ์˜ ์ž‘์—… ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์–ด์ง„ ์ž‘์—… ์šฐ์„ ์ˆœ์œ„ ๋ฐฐ์—ด๊ณผ ํŠน์ • ์œ„์น˜์˜ ์ž‘์—…์ด ๋ช‡ ๋ฒˆ์งธ๋กœ ์ถœ๋ ฅ๋˜๋Š”์ง€๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ณผ์ •์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ ํ•ด์„ค

ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ

Queue, LinkedList, Collections, Arrays ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ ์ •์˜

solution ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๊ณ , int[] priorities์™€ int location์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

๋ณ€์ˆ˜ answer๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

์ด๋Š” ์ตœ์ข…์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ๊ฐ’์œผ๋กœ, ํŠน์ • ์ž‘์—…์ด ๋ช‡ ๋ฒˆ์งธ๋กœ ์ถœ๋ ฅ๋˜๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

ํ ์ดˆ๊ธฐํ™”

Queue ํƒ€์ž…์˜ queue๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

location ์œ„์น˜์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ want_num ๋ณ€์ˆ˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

priorities ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ํ์— ์‚ฝ์ž…ํ•˜๋ฉด์„œ, want_num๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ count ๋ณ€์ˆ˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ count๊ฐ€ 0์ด๋ฉด, ์ด๋Š” want_num์ด ๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„์ž„์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ 1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์šฐ์„ ์ˆœ์œ„ ๋ฐฐ์—ด ์ •๋ ฌ

priorities ๋ฐฐ์—ด์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ arr ๋ฐฐ์—ด์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž‘์—… ์ˆœ์„œ ๊ณ„์‚ฐ

์ •๋ ฌ๋œ ๋ฐฐ์—ด arr์˜ ์š”์†Œ์™€ ํ์˜ ์•ž ์š”์†Œ๋ฅผ ๋น„๊ตํ•˜๋ฉด์„œ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

ํ์˜ ์•ž ์š”์†Œ๊ฐ€ ํ˜„์žฌ ์ฒ˜๋ฆฌํ•  ์šฐ์„ ์ˆœ์œ„์™€ ๊ฐ™์œผ๋ฉด answer๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ , ํ์—์„œ ํ•ด๋‹น ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

location์ด 0์ด๋ฉด, ์ด๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๋Š” ์ž‘์—…์ด๋ฏ€๋กœ ๋ฃจํ”„๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

ํ์˜ ์•ž ์š”์†Œ๊ฐ€ ํ˜„์žฌ ์ฒ˜๋ฆฌํ•  ์šฐ์„ ์ˆœ์œ„์™€ ๋‹ค๋ฅด๋ฉด ํ์˜ ์•ž ์š”์†Œ๋ฅผ ๋’ค๋กœ ๋ณด๋‚ด๊ณ , location ๊ฐ’์„ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

location์ด 0 ๋ฏธ๋งŒ์ด ๋˜๋ฉด ํ์˜ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๋ฐ˜ํ™˜

answer๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ํŠน์ • ์ž‘์—…์ด ๋ช‡ ๋ฒˆ์งธ๋กœ ์ถœ๋ ฅ๋˜๋Š”์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ

์˜ˆ๋ฅผ ๋“ค์–ด, priorities๊ฐ€ [2, 1, 3, 2]์ด๊ณ  location์ด 2๋ผ๋ฉด, ์ถœ๋ ฅ๋˜๋Š” ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

3 (์œ„์น˜ 2, ์ฒซ ๋ฒˆ์งธ ์ถœ๋ ฅ)

2 (์œ„์น˜ 0, ๋‘ ๋ฒˆ์งธ ์ถœ๋ ฅ)

2 (์œ„์น˜ 3, ์„ธ ๋ฒˆ์งธ ์ถœ๋ ฅ)

1 (์œ„์น˜ 1, ๋„ค ๋ฒˆ์งธ ์ถœ๋ ฅ)

location์ด 2์ธ ์ž‘์—…์€ ์ฒซ ๋ฒˆ์งธ๋กœ ์ถœ๋ ฅ๋˜๋ฏ€๋กœ, ๊ฒฐ๊ณผ๋Š” 1์ž…๋‹ˆ๋‹ค.