1. 문제 풀이
각 차량의 진출 지점에 대해서 내림차순으로 정렬합니다. 첫 단속카메라를 설치해야할 지점은 먼저 나간 차량의 진출 시점입니다. 그 후, 순회하면서 각 차량들의 진입, 진출 시점 사이의 단속카메라가 위치해있는지 확인하고 만약 없다면 그 차량의 진출 시점에 새로운 카메라를 설치해야합니다.
2. 코드
#include <bits/stdc++.h>
using namespace std;
using pi = pair<int, int>;
#define X first
#define Y second
vector<pi> lines;
int solution(vector<vector<int>> routes) {
for(auto &cur : routes) {
lines.push_back({cur[1], cur[0]});
}
sort(lines.begin(), lines.end());
int ans{1}, cur{lines[0].X};
for(int i = 1; i < lines.size(); i++) {
auto [cy, cx] = lines[i];
if(cx <= cur && cur <= cy) continue;
cur = cy;
ans++;
}
return ans;
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
프로그래머스 스킬체크 레벨 2 리뷰 (0) | 2025.01.05 |
---|---|
프로그래머스 Level 3 기지국 설치[C++] (0) | 2025.01.05 |
프로그래머스 Level3: 숫자게임[C++] (0) | 2025.01.03 |
프로그래머스 Level 3: 단어 변환[C++] (0) | 2025.01.01 |
프로그래머스 Level 3: 야근 지수 (0) | 2024.12.04 |