[프로그래머스][단속카메라]-Lv.3

호준·2022년 11월 16일
0

Algorithm

목록 보기
104/111
post-thumbnail

🧨 문제

링크텍스트

🧨 제한사항

🧨 접근방법

  1. 끝나는 시간 기준으로 오름차순 정렬을 한다.
  2. std를 Integer 최솟값을 지정해준다.
  3. routes 조회하면서 시작지점이 std보다 클 경우 카메라 개수 +1 해주고 시작지점을 std로 지정한다.

🧨 코드

import java.util.*;
class Solution {
    public int solution(int[][] routes) {
        int answer = 0;
        Arrays.sort(routes, (o1,o2) -> o1[1]-o2[1]);
        
        int std = Integer.MIN_VALUE;
        for(int i=0; i<routes.length; i++){
            if(std < routes[i][0]){
                std = routes[i][1];
                answer++;
            }
        }
        return answer;
    }
}
profile
도전하지 않는 사람은 실패도 성공도 없다

0개의 댓글