본문 바로가기

algorithm/프로그래머스

프로그래머스_단속카메라(JAVA)

https://school.programmers.co.kr/learn/courses/30/lessons/42884

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

[JAVA]

import java.util.*;

class Solution {
    public int solution(int[][] routes) {
        int answer = 1;
        
        //도로의 끝나는 지점을 기준으로 정렬합니다.
        Arrays.sort(routes, (o1, o2) -> o1[1] - o2[1]);
        
        int current_end = routes[0][1];
        
        for(int i = 0; i < routes.length; i++){
            
            //현재 끝나는 지점 보다 시작 지점이 빠른 경우 현재 끝나는 지점을 갱신합니다.
            if(routes[i][0] > current_end){
                current_end = routes[i][1];
                answer++;
            }
        }
        
        return answer;
    }
}