Development Palette
1828 냉장고 본문
package com.jungol.n1828_냉장고;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
// 중복되는(겹치는) 부분으로 냉장고 온도를 설정하는것!!.. 문제를 이해하기 어려웠넴
//high 기준 오름차순 정렬
public class Main {
static class Refrigerator implements Comparable<Refrigerator>{
int low, high;
public Refrigerator(int low, int high) {
super();
this.low = low;
this.high = high;
}
@Override
public int compareTo(Refrigerator o) { //high 기준 오름차순 정렬
int value = this.high - o.high;
return value;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
Refrigerator [] re = new Refrigerator[N];
for(int n = 0; n<N; n++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken());
int h = Integer.parseInt(st.nextToken());
re[n] = new Refrigerator(l, h);
}
bw.append(getCount(re)+" ");
bw.flush();
bw.close();
}
static int getCount (Refrigerator [] re) {
int cnt = 1; //처음 냉장고는 무조건 한개부터 시작이므로
ArrayList<Refrigerator> list = new ArrayList<>();
Arrays.sort(re); //high 기준 오름차순 정렬
list.add(re[0]); //처음 냉장고는 무조건 한개부터 시작이므로
int temp = re[0].high; //냉장고의 기준 온도를 // 최고온도가 가장 낮은 물건의 최고온도로 설정
for(int r=1, size = re.length; r<size; r++) { //보관할 물건 갯수 만큼 반복
list.add(re[r]);
if(temp < re[r].low) { // 온도가 겹치지 않을 때
temp = list.get(list.size()-1).high; //냉장고의 기준 온도를 // 저장할 물건 이전의 최고온도로 변경
cnt++; //냉장고 갯수 증가
}
}
return cnt;
}
}
'Algorithm > JUNGOL' 카테고리의 다른 글
j1681_해밀턴순환회로 (0) | 2021.09.24 |
---|---|
1037_오류교정 (0) | 2021.08.26 |
Comments