🌞Algorithm/🔥programmers

[programmers] 프로세스

뿌야._. 2026. 8. 25. 10:39
문제
https://school.programmers.co.kr/learn/courses/30/lessons/42587
 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 


< 프로세스 >

 

문제 풀이 (Java)

import java.util.*;

class Solution {
	public int solution(int[] priorities, int location) {
		int answer = 0;

		Queue<int[]> queue = new LinkedList<>();
		ArrayList<Integer> list = new ArrayList<>();

		for (int i = 0; i < priorities.length; i++) {
			queue.add(new int[] { i, priorities[i] });
			list.add(priorities[i]);
		}

		Collections.sort(list, Collections.reverseOrder());
		for (int i = 0; i < list.size(); i++) {
			while (list.get(i) != queue.peek()[1]) {
				queue.add(queue.poll());
			}
			int temp[] = queue.poll();
			if (temp[0] == location) {
				answer = i + 1;
			}
		}

		return answer;
	}
}

 

Queue에 [현재 프로세스 위치, 우선순위]를 저장하고 ArrayList에는 우선순위만 저장한다. 우선순위를 내림차순으로 정렬한 것을 순회하며 다음 과정을 반복한다.

 

1. 우선순위와 Queue의 peek 값이 일치하는지 확인 후 일치하지 않다면 poll하여 맨 뒤에 넣는다.

2. 일치하다면 poll한다

3. 만약 poll한 값의 원래 위치가 location과 일치하다면 answer을 업데이트한다.

 

최종 answer을 반환한다. 

 



 

출처: 프로그래머스 코딩 테스트 연습, 
https://school.programmers.co.kr/learn/challenges

'🌞Algorithm > 🔥programmers' 카테고리의 다른 글

[programmers] 최소직사각형  (0) 2026.08.26
[programmers] 기능개발  (0) 2026.08.24
[programmers] 구명보트  (0) 2026.08.20
[programmers] 체육복  (0) 2026.08.19
[programmers] 모의고사  (0) 2026.08.18