GaGe

[프로그래머스] 단속카메라 - java 본문

알고리즘/[2020] 프로그래머스

[프로그래머스] 단속카메라 - java

Sorrel 2020. 7. 17. 18:26
import java.util.*;
class Solution {
    public int solution(int[][] routes) {
        int answer = 1;
        
        Arrays.sort(routes, new Comparator<int[]>(){
            public int compare(int[]o1, int[]o2){
                if(o1[0]==o2[0])
                    return Integer.compare(o1[1],o2[1]);
                return Integer.compare(o1[0],o2[0]);
            }
        });
        int result = routes[0][1];
        for(int i =1; i<routes.length-1 ; i++){
            if(result>routes[i][1]){
                result = routes[i][1];
            }
            if(result<routes[i+1][0]){
                answer++;
                result=routes[i+1][1];
            }
        }
        
        return answer;
    }
}
Comments