inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

자바(Java) 알고리즘 문제풀이 입문: 코딩테스트 대비

8. 응급실

프로그래머스 프린터와 같은 문제네요!! 깔끔한 코드 공유합니다!

468

김태우
0

프로그래머스에서 풀때 어쩔수 없이 class를 썻던 기억이 있는데 실제로 id값을 줘서 푸네요.

 

 

 

public class Main {
public int solution(int[] arr, int k) {

Queue<Person> queue = new ArrayDeque<>();

for (int i = 0; i < arr.length; i++) queue.offer(i == k ? new Person(-1, arr[i]) : new Person(arr[i]));
int day = 1;

while (!queue.isEmpty()) {
Person p = queue.poll();

int emergency = Integer.parseInt(String.valueOf(queue.stream().filter(x -> x.number > p.number).count()));

if (emergency == 0 && p.id != -1) day++;
else if (emergency == 0 && p.id == -1) return day;
else queue.add(p);
}
return day;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();
int k = sc.nextInt();

int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();

System.out.println(new Main().solution(arr, k));
}
}

 

답변 0