[BOJ] 2594 놀이공원

알파·2022년 7월 20일
0

분 단위로 바꿔서 시작시간은 10분 일찍 종료시간은 10분 늦게 저장해준다. 안그러면 계산이 너무 복잡해져서 풀기 힘들다!!
가장 늦은 종료시간을 갱신해주면서 시작시간이 그보다 늦을 경우 값을 계산한다.
그리고 마지막으로 종료시간 기준으로 정렬해준 다음 가장 늦은 종료시간과 22시를 한번 더 계산하면 된다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class Main {
    static int N, max, endMax;
    static int[][] arr;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        arr = new int[N][2];
        StringTokenizer st;
        for(int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            int start = Integer.parseInt(st.nextToken());
            int end = Integer.parseInt(st.nextToken());
            arr[i][0] = (start / 100) * 60 + (start % 100) - 10;
            arr[i][1] = (end / 100) * 60 + (end % 100) + 10;
        }
        Arrays.sort(arr, Comparator.comparingInt(o -> o[0]));
        max = Math.max(max, arr[0][0] - 600);

        for(int i = 1; i < N; i++) {
            endMax = Math.max(endMax, arr[i-1][1]);
            if(arr[i][0] > endMax) {
                max = Math.max(max, arr[i][0] - endMax);
            }
        }

        Arrays.sort(arr, Comparator.comparingInt(o -> o[1]));
        max = Math.max(max,  1320 - arr[N-1][1]);

        System.out.println(max);
    }
}
profile
I am what I repeatedly do

0개의 댓글