๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[C++/PGS] Lv.2 : ์š”๊ฒฉ ์‹œ์Šคํ…œ

by xxilliant 2025. 6. 18.
728x90
๋ฐ˜์‘ํ˜•

 

https://school.programmers.co.kr/learn/courses/30/lessons/181188

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก์˜ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr


ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ ˆ๋ฒจ2.

๊ทธ๋ฆฌ๋”” ์œ ํ˜•์˜ ๋ฌธ์ œ์ด๋‹ค

์ฒ˜์Œ์— map์œผ๋กœ ์‚ฝ์งˆํ•˜๋‹ค๊ฐ€ ํžŒํŠธ ๋ณด๊ณ  ํ•ด๊ฒฐํ–ˆ๋‹ค๐Ÿฅน

 

ํƒ€๊ฒŸ์˜ ์‹œ์ž‘์ ์ด ๊ธฐ์กด์˜ ๋์  ์ด์ƒ์ด๋ผ๋ฉด answer+1์„ ํ•ด์ฃผ๋ฉด ๋‹ต์ด ๋‚˜์˜จ๋‹ค!

 

๋‚˜์˜ ํ’€์ด

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

bool comp(vector<int> a, vector<int> b){
    if(a[1]==b[1]) return a[0]<b[0];
    return a[1]<b[1];
}

int solution(vector<vector<int>> targets) {
    int answer = 0;
    int end = 0;
    sort(targets.begin(), targets.end(), comp);
    for(int t=0; t<targets.size(); ++t){
        if(end <= targets[t][0]){
            answer++;
            end = targets[t][1];
        }
    }
    return answer;
}

728x90
๋ฐ˜์‘ํ˜•