728x90
문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/42884
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
코드 구현
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool Compare(const vector<int>& a, const vector<int>& b)
{
return a[1] < b[1];
}
int solution(vector<vector<int>> routes)
{
int answer = 1;
// 진출 시점 기준으로 오름차순 정렬
sort(routes.begin(), routes.end(), Compare);
int out = routes[0][1];
for (const auto& route : routes)
{
if (out >= route[0] && out <= route[1]) continue;
else
{
out = route[1];
answer++;
}
}
return answer;
}
해결 방법
- 진출 시점을 기준으로 오름차순으로 정렬
- 첫 번째 진출 시점에 카메라를 설치하고 다음 차량들을 순회하면서 카메라가 다음 차량의 범위 안에 없으면 카메라를 추가한다
'개발 > 알고리즘' 카테고리의 다른 글
| [백준] 2252번: 줄 세우기(C++) (1) | 2025.06.24 |
|---|---|
| [백준] 1806번: 부분합(C++) (1) | 2025.06.13 |
| [백준] 2206번: 벽 부수고 이동하기(C++) (1) | 2025.06.09 |
| [프로그래머스] 이중우선순위큐(C++) (1) | 2025.06.08 |
| [백준] 9663번: N-Queen(C++) (1) | 2025.05.31 |