1828 냉장고

2020. 2. 10. 17:21Learn/Algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        boolean[] arr = new boolean[10271]; // 1 ~ 10000
        DOT[] input = new DOT[N];
        int result = 0;
        for (int i = 0; i < N; i++) {
            int min = sc.nextInt();
            int max = sc.nextInt();
            min +=270;
            max += 270;
            input[i] = new DOT(min,max);
         }
        Arrays.sort(input); // 정렬
        int xmin = input[0].start;
        int xmax = input[0].end;
        int cnt = 1;
        for (int i = 1; i < input.length; i++) {
            if(input[i].end < xmin || input[i].start > xmax ) {
                cnt++;
                xmin = input[i].start;
                xmax = input[i].end;
            }
            else {
                xmin = Math.max(xmin, input[i].start);
                xmax = Math.min(xmax, input[i].end);
            }
        }
        System.out.println(cnt);
    }
    public static class DOT implements Comparable<DOT>{
        int start,end;
        public DOT(int start,int end) {
            this.start = start;
            this.end = end;
        }
        public int compareTo(DOT d) {
            return this.start - d.start;
        }
    }
}
        
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

너무 어려웠던 문제,

정렬해준뒤 겹치는 부분을 넘어가면 수가 증가하도록 설정하였다.

N은 무조건 1 이상이므로 cnt의 초기값은 1을 주었다.

'Learn > Algorithm' 카테고리의 다른 글

2493 탑  (0) 2020.02.11
1861 정사각형 방  (0) 2020.02.10
1873. 상호의 배틀필드  (0) 2020.02.10
4408 자기방으로 돌아가기  (0) 2020.02.10
14501번 퇴사코드 - DP  (0) 2020.02.07