본문 바로가기
Algorithm Study/Programmers (JAVA)

프로그래머스 Lv3_단속카메라_Java

by 창브로 2024. 7. 7.
728x90

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

 

프로그래머스

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

programmers.co.kr

 

import java.util.*;

class Solution {
    public int solution(int[][] routes) {
         // 차량의 이탈 지점을 기준으로 오름차순 정렬
        Arrays.sort(routes, (o1, o2) -> o1[1] - o2[1]);
        // 첫 번째 카메라를 첫 번째 차량의 이탈 지점에 설치
        int location = routes[0][1];
        int answer = 1;
        
        for (int i = 1; i < routes.length; i++) {
             // 현재 설치된 카메라로 감시할 수 없는 차량이 나타나면
            if (routes[i][0] > location) {
                 // 새로운 카메라를 해당 차량의 이탈 지점에 설치
                answer++;
                location = routes[i][1];
            }
        }
        return answer;
    }
}

 

 

회고

- 생각하는건 쉬웠지만 코드 구현이 쉽지 않았다