Muscardinus

[프로그래머스] 단속카메라 (Lv 3) 본문

알고리즘 문제/[프로그래머스] Lv3

[프로그래머스] 단속카메라 (Lv 3)

Muscardinus 2020. 8. 17. 20:53
728x90

 

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

 

코딩테스트 연습 - 단속카메라

[[-20,15], [-14,-5], [-18,-13], [-5,-3]] 2

programmers.co.kr

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
Comments