C++/programmers 코딩테스트(level 3) C++

[C/C++] programmers Level 3 단속카메라 (복습)

CE : 하랑 2023. 11. 8. 17:06

 

 

 

 

 

코드

 

#include <vector>
#include <algorithm> // sort

using namespace std;

int solution(vector<vector<int>> routes) {
    int answer = 1; // 기본 카메라 1대
    
    sort(routes.begin(), routes.end()); // 오름 차순 정렬
    
    int check = routes[0][1]; // 현재 차가 나가는 시점
    
    for(const auto& r : routes){
        
        if(check<r[0]){ // 현재 차가 나가는 시점보다 뒤 차가 들어오는 시점이 클 때
            answer++; // 카메라 추가
            check=r[1]; // 뒤 차가 나가는 시점으로 정정 
        }
        
        if(check>r[1]){ // 현재 차가 나가는 시점이 뒤 차가 나가는 시점보다 클 때
            check=r[1]; // 뒤 차가 나가는 시점으로 정정  
        }
    }
    
    return answer;
}