[알고리즘] 인프런 - 응급실

정은아·2024년 1월 13일
post-thumbnail

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

- Section 5 - 응급실 문제

내 풀이

import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Stack_08 {
static class Person{
		int id;
		int priority;
		public Person(int id, int priority) {
			this.id = id;
			this.priority = priority;
		}
	}
	
static class Bomin{
		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.add(new Person(i, arr[i]));
			}
			
			while(!Q.isEmpty()) {
				Person tmp = Q.poll();
				for (Person x : Q) {
					if (x.priority > tmp.priority) {
						Q.add(tmp);
						tmp = null;
						break;
					}
				}
				
				if (tmp != null) {
					answer++;
					if (tmp.id == m) {
						return answer;
					}
				}
			}
			
			return answer;
		}
	}
	public static void main(String[] args) throws IOException{
		// M명의 환자 중, N번째 환자가 몇 번째로 진료받는지 구하는 문제
		
		// 1. 
		Bomin T = new Bomin();
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int m = sc.nextInt();
		int [] arr = new int[n];
		
		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		
		System.out.println(T.solution(n, m, arr));
	}
}

다음 날 다시 풀기

package Stack_Queue;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Queue_08_retry {
    public static class Person{
        int id;
        int risk;

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

    public static class Patient{
        public static int Solution(int M, int N, int [] arr){
           int answer  = 0;
            Queue<Person> queue = new LinkedList<>();

            for (int i = 0; i < M; i++){
                queue.add(new Person(i, arr[i]));
            }

            while(!queue.isEmpty()){
                Person tmp = queue.poll();
                for (Person x: queue) {
                    if (x.risk > tmp.risk){
                        queue.add(tmp);
                        tmp = null;
                        break;
                    }
                }

                if (tmp != null) {
                    answer++;
                    if (tmp.id == N) {
                        return answer;
                    }
                }
            }
            return answer;
        }
    }
    public static void main(String[] args) {
        // M명의 환자 중, N번째 환자가 몇 번째로 진료받는지 구하는 문제

        // 1. 테스트케이스 int M,N을 받고 배열을 만들어 채운다.
        // 2. 환자값에 두개의 값이 들어가므로 class와 생성자를 만든다.
        // 3. 나머지 식을 전개하기 위한 class를 새로 만든다.
        // 4. queue를 만들고, 값이 2개 이므로 for문을 돌려 채운다.
        // 5. 맨 왼쪽에 있는 환자를 일단 poll하고 가장 위험한 환자인지 비교한다.
        // 6. 가장 위험한 환자가 아니라면 다시 맨오른쪽으로 add()한다.
        // 7. 가장 위험한 환자라면 그대로 poll()하고 answer++한다.
        // 8. 궁금해하던 환자의 순서가 몇번째인지 구한다.(그대로 answer을 return한다.)

        Patient patient = new Patient();
        Scanner sc = new Scanner(System.in);

        int M = sc.nextInt();
        int N = sc.nextInt();
        int [] arr = new int [M];

        for (int i = 0; i < M; i++){
            arr[i] = sc.nextInt();
        }

        System.out.println(patient.Solution(M, N, arr));

        sc.close();

    }
}

느낀점

스터디 팀원인 희석님과 보민님이 나서주셔서 해결해주셨다..
인텔리제이에서 class 앞에 static을 써야한다고 하는데 인텔리제이 진짜 똑똑하다
클래스는 어떻게 만드는걸까 이제 더 이상 클래스 만드는걸 망설이면 안되는데,
빨리 익혀야겠다. 하 어려워~

profile
꾸준함의 가치를 믿는 개발자

0개의 댓글