[JUNGOL] 냉장고

ERror.ASER·2021년 2월 17일
0

JUNGOL

목록 보기
1/1
post-thumbnail

초등학교때 많이 그린 범위를 나타내는 선을 그려보면 간단하게 해결할 수 있다.

package com.study.classAlgo;

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

public class JUNGOL1828 {

    public static class Point implements Comparable<Point> {
        int start;
        int end;
 
        public Point(int start, int end) {
            this.start = start;
            this.end = end;
        }
 
        @Override
        public int compareTo(Point o) {
            return this.end - o.end;
        }
    }
	
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		Point[] chemicals = new Point[n];
		
		for(int i=0; i<n; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int s = Integer.parseInt(st.nextToken());
			int e = Integer.parseInt(st.nextToken());
			chemicals[i] = new Point(s,e);
		}
		
		Arrays.sort(chemicals);
		
		Point temp = chemicals[0];
		int count = 1;
		
		for(int i=1; i<n; i++) {
			if(temp.end<chemicals[i].start) {
				count++;
				temp = chemicals[i];
			}
		}
		
		System.out.println(count);	
	}
}
profile
지우의 블로그

0개의 댓글