본문 바로가기
알고리즘/JAVA

8. 응급실

by vivi 2021. 9. 23.
import java.util.*;

class Person {
	int id;
	int priority;

	public Person(int id, int priority) {
		this.id = id;
		this.priority = priority;
	}
}

class Main {
	public int solution(int N, int M, Queue<Person> q) {
		int answer = 1;
		while (!q.isEmpty()) {
			Person tmp = q.poll();
			for (Person x : q) {
				if (tmp.priority < x.priority) {
					q.offer(tmp);
					tmp = null;
					break;
				}
			}
			if (tmp != null) {
				if (tmp.id == M)
					return answer;
				else
					answer++;
			}
		}
		return answer;
	}

	public static void main(String[] args) {
		Main T = new Main();
		Scanner kb = new Scanner(System.in);
		int N = kb.nextInt();
		int M = kb.nextInt();
		Queue<Person> q = new LinkedList<>();
		for (int i = 0; i < N; i++) {
			q.offer(new Person(i, kb.nextInt()));
		}
		System.out.print(T.solution(N, M, q));
		kb.close();
	}
}

'알고리즘 > JAVA' 카테고리의 다른 글

2. 버블 정렬  (0) 2021.09.23
1. 선택 정렬  (0) 2021.09.23
7. 교육과정 설계  (0) 2021.09.23
6. 공주 구하기  (0) 2021.09.23
5. 쇠막대기  (0) 2021.09.23

댓글