[정올] 1828. 냉장고 (java, 그리디)

728x90
반응형

www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=1101&sca=99&sfl=wr_hit&stx=1828

 

JUNGOL

 

www.jungol.co.kr

아직도 잘 이해가 안됨 

냉장고의 최고 기온 보다 화학물질의 최저 기온이 높을 때 냉장고 최고 기온을 갱신해준다.

 

온도 범위가 겹치는 부분이 있으면 그 사이의 온도중 특정 온도로 유지하면 둘 다 산다.

 

하지만 겹치지 않는 경우 냉장고 최고 기온보다 화학물질의 최저 기온 기준이 높을 때는 서로 겹치는 온도 범위가 없으

 

므로 새로운 냉장고가 필요하다.

 


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static int n;

	static class Temp implements Comparable<Temp>{
		int low,high;

		public Temp(int low, int high) {
			super();
			this.low = low;
			this.high = high;
		}

		@Override
		public int compareTo(Temp o) {
			return this.high-o.high;
		}
		
	
	}
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		n=sc.nextInt();
		Temp[] t=new Temp[n];
		for (int i = 0; i < n; i++) {
			t[i]=new Temp(sc.nextInt(),sc.nextInt());
		}
		Arrays.sort(t);
		int cnt=1;

		int max=t[0].high;
		for (int i = 1; i < t.length; i++) {
			if(t[i].low>max) {
				max=t[i].low;
				cnt+=1;
			}
		}
		System.out.println(cnt);
		
		
	}
}
728x90
반응형
TAGS.

Comments