Muscardinus
[프로그래머스] 단속카메라 (Lv 3) 본문
728x90
https://programmers.co.kr/learn/courses/30/lessons/42884
C++
#include <vector>
#include <algorithm>
using namespace std;
struct pos {
int st,end;
pos(int a, int b) {
st=a; end=b;
}
bool operator < (const pos &b) const {
return end < b.end;
}
};
int solution(vector<vector<int>> routes) {
int answer = 0;
vector<pos> v;
for(int i=0;i<routes.size();i++){
v.push_back(pos(routes[i][0],routes[i][1]));
}
sort(v.begin(),v.end());
int init=-30000;
for(int i=0;i<v.size();i++){
if(init<v[i].st){
init=v[i].end;
answer++;
}
}
return answer;
}
JavaScript
function solution(routes) {
var answer = 0;
let init=-30000;
routes.sort((a,b) => {return a[1] - b[1];});
for(let i=0;i<routes.length;i++){
if(init<routes[i][0]){
init=routes[i][1];
answer++;
}
}
return answer;
}
728x90
'알고리즘 문제 > [프로그래머스] Lv3' 카테고리의 다른 글
N으로 표현 (0) | 2021.02.12 |
---|---|
[프로그래머스] 여행경로 (Lv3) (0) | 2020.09.09 |
[프로그래머스] 단어변환 (Lv3) (0) | 2020.09.08 |
[프로그래머스] 2 x n 타일링 (Lv 3) (0) | 2020.08.26 |
[프로그래머스] 네트워크 (Lv 3) (0) | 2020.08.18 |
Comments