본문 바로가기

코딩테스트/프로그래머스

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

728x90
반응형


🔗 문제 링크

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


반응형
728x90

👩‍💻 코드

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

int solution(vector<vector<int>> routes) {
    int answer = 1;
    
    sort(routes.begin(), routes.end(), [](const vector<int> &a, vector<int> &b){
        return a[1] < b[1];
    });
    
    int camera = routes[0][1];
    
    for(int i=1; i<routes.size(); i++){
        if(routes[i][0] > camera){
            answer++;
            camera = routes[i][1];
        }
    }
    
    return answer;
}

📝 풀이

우선 차량의 진출 지점을 기준으로 routes를 오름차순 정렬을 합니다.

첫 번째 차량의 진출 지점에 카메라를 설치합니다.

이후 차량의 진입 지점이 카메라에 잡히지 않으면(겹치지 않으면) 진출 지점에 새로운 카메라를 설치합니다.

최종적으로 설치된 카메라의 개수 answer을 return 합니다.

728x90
반응형

'코딩테스트 > 프로그래머스' 카테고리의 다른 글

[프로그래머스] 입국심사  (0) 2024.10.26
[프로그래머스] 순위  (0) 2024.10.26
[프로그래머스] 베스트앨범  (1) 2024.10.25
[프로그래머스] 도둑질  (0) 2024.10.25
[프로그래머스] 등굣길  (2) 2024.10.24