• Home

My Codegate

  • Home

프로그래머스 프로세스 자바 문제풀이

2023/12/12 Posted by Codegate Java No Comments

문제 링크

문제 내용은 우선 순위를 받아서 숫자가 높은 순서대로 진행하되, location으로 받은 인덱스 값이 언제 사용되는지를 Return 해주면 끝인데

import java.util.Comparator;
import java.util.PriorityQueue;

public class Process {

  public static int solution(int[] priorities, int location) {
    int answer = 0;
    PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
    for (int i = 0; i < priorities.length; i++) {
      queue.add(priorities[i]);
    }

    while (!queue.isEmpty()) {
      for (int i = 0; i < priorities.length; i++) {
        if (queue.peek() == priorities[i]) {
          if (i == location) {
            answer++;
            return answer;
          } else {
            queue.poll();
            answer++;
          }
        }
      }
    }

    return answer;
  }

  public static void main(String[] args) {
    int[] priorities = {1, 1, 9, 1, 1, 1};
    System.out.println(solution(priorities, 0));
  }

}

문제는 단순하지만 우선순위 큐를 이용하지 않으면 풀 수가 없다. 값이 큰 순서대로 들어가도록 우선순위 큐를 생성한 후 값을 쭉 넣어주고

이후 while을 돌려가면서 우선순위 큐에 넣은 최대값 순서대로 poll 시켜줘야 하는데 여기서 priorities[] 배열과 값이 동일한지 체크해주면서 순서에 해당하는 answer 값을 추가해가며 뽑되, 뽑을 인덱스가 location에 해당하는 값이라면 바로 answer에 1 더해준 후 return 시켜주면 끝난다.

No Comments
0

Leave a Reply Cancel Reply

Introduction

My Codegate

Latest Posts

  • Google Search Console API 연동방법
  • 인텔리제이 Gradle Dependency 최신 버전 보는 방법
  • Wallet-Tracker 개발일지
  • Moralis API 자바로 호출방법
  • IntelliJ Commit 후 Push 따로 하는 방법

Categories

  • My Project (4)
  • Java (42)
  • Algorithm (161)
    • Java (152)
    • Algorithm Knowledge (3)
    • Algorithm site usage (6)
  • Vue.js (1)
  • Spring (4)
  • Docker (2)
  • IntelliJ (20)
  • Uncategorized (7)

Recent Comments

  • Codegate on Hello world!
  • A WordPress Commenter on Hello world!

© 2025 — mycodegate.com

Prev Next