Playground/자바문제집

[백준] 14469번

미숫가루설탕많이 2023. 5. 22. 20:18

소가 들어오는 시간과 통과하는 데 걸리는 시간을 배열로 만들어서 오름차순으로 정렬해준다. 그리고 배열을 순회하면서 바로 앞의 소가 검문받는 중이면 기다리는 시간만큼 추가해줬다.

 

 

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        int N = Integer.parseInt(br.readLine());
        int[][] line = new int[N][2];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            line[i][0] = Integer.parseInt(st.nextToken());
            line[i][1] = Integer.parseInt(st.nextToken());
        }

        Arrays.sort(line, Comparator.comparingInt(o1 -> o1[0]));
        int time = line[0][0] + line[0][1];
        for (int i = 1; i < N; i++) {
            if (line[i][0] >= time) {
                time = line[i][0] + line[i][1];
            } else {
                time = line[i][0] + line[i][1] + (time - line[i][0]);
            }
        }

        System.out.println(time);
    }
}

'Playground > 자바문제집' 카테고리의 다른 글

[백준] 5430번  (2) 2023.06.09
[백준] 1049번  (0) 2023.05.23
[백준] 11724번  (2) 2023.05.21
[백준] 2667번  (0) 2023.05.18
[백준] 1697번  (0) 2023.05.17