보라코딩

백준 자바 :: 공주님의 정원 (그리디) 본문

백준(java)

백준 자바 :: 공주님의 정원 (그리디)

new 보라 2024. 8. 29. 12:40

정렬을 이용해 풀어야 한다고 생각했는데

꼭 그러지 않아도 되었다.

 

 

 

 

 

 

 

 

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

public class Main{

    // 공주님의 정원 (그리디)

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        StringTokenizer st;

        List<int[]> flowers = new ArrayList<>();
       
        for(int i = 0; i < N; i++){
            st = new StringTokenizer(br.readLine());
            int startMonth = Integer.parseInt(st.nextToken());
            int startDay = Integer.parseInt(st.nextToken());
            int endMonth = Integer.parseInt(st.nextToken());
            int endDay = Integer.parseInt(st.nextToken());
            // 월 * 100 + 일 (1203 이렇게)
            flowers.add(new int[]{startMonth*100 + startDay, endMonth*100 + endDay});
        }

        int current = 301; // 현재 시간
        int answer = 0;

        while (current < 1201){ // 11월 30일까지
            int next = current;
            for (int[] flower : flowers){
                if(flower[0] <= current && flower[1] > next){
                    next = flower[1];
                }
            }
            if (next == current){
                System.out.println(0);
                return;
            }
            answer++;
            current = next;
        }
        System.out.println(answer);
    }
}