231018 프로세스

Jongleee·2023년 10월 18일
0

TIL

목록 보기
393/576
public int solution(int[] priorities, int location) {
	Queue<Integer> queue = new LinkedList<>();
	int[] counts = new int[10];
	int answer = 0;

	for (int i = 0; i < priorities.length; i++) {
		queue.offer(i);
		counts[priorities[i]]++;
	}

	int priority = 9;

	while (!queue.isEmpty()) {
		int currentDoc = queue.poll();

		while (counts[priority] == 0) {
			priority--;
		}

		if (currentDoc == location && priority == priorities[currentDoc]) {
			answer++;
			break;
		}

		if (priorities[currentDoc] < priority) {
			queue.offer(currentDoc);
		} else {
			answer++;
			counts[priorities[currentDoc]]--;
		}
	}

	return answer;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/42587

0개의 댓글