๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[C++/PGS] Lv.3 : ์ˆซ์ž ๊ฒŒ์ž„ (๊ทธ๋ฆฌ๋”” Greedy)

728x90

 

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

 

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

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

programmers.co.kr

 

 

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

๊ทธ๋ฆฌ๋””๋กœ ํ•ด๊ฒฐํ–ˆ๋‹ค

 

์ค‘์š” ํฌ์ธํŠธ = ์ •๋ ฌ & ์กฐ๊ฑด์— ๋งž์ง€ ์•Š๋Š” ์š”์†Œ ์‚ญ์ œ

 

 

 

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

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

int solution(vector<int> A, vector<int> B) {
    int answer = 0;
    // ๊ทธ๋ฆฌ๋””? o ์™„ํƒ? x
    sort(A.begin(), A.end(), greater<int>());
    sort(B.begin(), B.end(), greater<int>());
    
    for(int i=0; i<A.size(); ++i){
        int now = A[i];
        if(A[i]<B[i]) answer++;
        else {
            A.erase(A.begin()+i);
            i--;
        }
    }
    return answer;
}

 

 

728x90