프로그래머스 LEVEL 3(단속카메라)

image

  • 사용 언어 : javascript

  • 해결 날짜 : 2022-10-22

  • 해결 방법 :
    • 차량의 진출 시점을 기준으로 오름차순 정렬
    • 카메라 설치 위치 temp -30001로 초기화(차량의 진출 시점이 -30000부터 시작)
    • 정렬된 routes를 돌며 현재 차량의 진입 시점이 temp보다 크다면 새 카메라를 설치해야 하므로 answer 증가 및 temp값 현재 차량의 진출 시점으로 update
  • 회고 :
    • x
  • 코드

    function solution(routes) {
        let answer = 0;
        routes.sort((a, b) => a[1] - b[1]);
        let temp = -30001;
            
        for (const route of routes) {
            if (temp < route[0]) {
                answer++;
                temp = route[1];
            }
        }
        return answer;
    }
    
  • 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges