• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

8. 응급실 문제에서 큐에 넣어준 임시변수를 null 값을 새로 할당할 필요가 없지 않을까요?

23.07.28 23:41 작성 23.07.28 23:42 수정 조회수 253

0

 

while문 안의 for문 안에서 if문에 큐에 넣어준 변수 temp에 null을 할당하지 않아도 로직상으로 문제가 안된다고 생각하는데, 그렇게 생각해도 될까요?

 

import java.util.*;
import java.io.*;

class Person {
int id;
int priority; // 위험도

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

public class Main {
public int solution(int n, int m, int[] arr) {
int answer = 0;
Queue<Person> q = new LinkedList<>();

for (int i = 0; i < n; i++) {
q.offer(new Person(i, arr[i]));
}

while (!q.isEmpty()) {
Person temp = q.poll();
for (Person person : q) {
if (person.priority > temp.priority) {
q.offer(temp);
// temp = null;
break;
}
}
if (temp != null) {
answer++;

if (temp.id == m) {
return answer;
}
}
}

return answer;
}

public static void main(String[] args) throws IOException {
Main main = new Main();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());

int[] arr = new int[n];

st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}

System.out.println(main.solution(n, m, arr));
}
}

답변 1

답변을 작성해보세요.

0

안녕하세요^^

temp에 null을 넣어주어야 밑에 if(temp != null) 현재 이 환자가 진료를 받을 수 있는지 없는지를 판별할 수 있습니다. 위에 코드로 채점해보세요. 통과가 안될 겁니다.

아래 테스트 케이스로 해보세요.

6 0

60 60 90 60 60 60

답은 5입니다.