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

[C++/PGS] [PCCP ๋ชจ์˜๊ณ ์‚ฌ #1] 2๋ฒˆ - ์ฒด์œก๋Œ€ํšŒ

by xxilliant 2025. 5. 2.
728x90
๋ฐ˜์‘ํ˜•

 

https://school.programmers.co.kr/learn/courses/15008/lessons/121684

 

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

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

programmers.co.kr


ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค PCCP ๋ชจ์˜๊ณ ์‚ฌ 1ํšŒ - 2๋ฒˆ

์†Œ์š”์‹œ๊ฐ„ ์•ฝ 23๋ถ„

์ˆœ์—ด(permutation) ํ˜น์€ dfs/์žฌ๊ท€ ์œ ํ˜•, ์ถ”์ • ๋‚œ์ด๋„๋Š” level 2-3

 

๋‚ด๊ฐ€ C++์„ ์ข‹์•„ํ•˜๋Š” ์ด์œ ,,, 
์ˆœ์—ด๊ฐ™์€ ๊ธฐ๋ณธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ ๋ถ€๋‹ด์„ ์ค„์—ฌ์ฃผ๋Š” ํ‘œ์ค€ ํ•จ์ˆ˜๊ฐ€ ํ’๋ถ€ํ•˜๋‹ค!!

 

ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ <algorithm> next_permutation ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

โš ๏ธ ์ˆœ์—ด ํ•จ์ˆ˜ ์‚ฌ์šฉ์‹œ์—๋Š” ๋ฐ˜๋“œ์‹œ ์ •๋ ฌ ํ›„, do~while ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์จ์ค˜์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด ๋ฌธ์ œ์—์„œ๋Š” ability ๋ฒกํ„ฐ๋ฅผ reverseํ•ด์ค˜์•ผ ํ•จ (์šด๋™์ข…๋ชฉ ๊ธฐ์ค€์œผ๋กœ ์ˆœ์—ด ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ)

 

 

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

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

int solution(vector<vector<int>> ability) {
    int answer = 0;
    // ์ตœ๋Œ“๊ฐ’, ๊ทธ๋ฆฌ๋””? ๊ฐ ์ข…๋ชฉ๋ณ„๋กœ ์ˆœ์„œ ๋‹ค๋ฅด๊ฒŒ
    int students = ability.size();
    int sports = ability[0].size();
    vector<vector<int>> reverse;
    // ์ˆœ์—ด์„ ์œ„ํ•ด ๋ฒกํ„ฐ ๋’ค์ง‘๊ธฐ
    for(int i=0; i<sports; ++i){
        vector<int> v;
        for(int j=0; j<students; ++j) v.push_back(ability[j][i]);
        reverse.push_back(v);
    }
    // ์ˆœ์—ด ํ™œ์šฉํ•ด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ํƒ์ƒ‰
    sort(reverse.begin(), reverse.end());
    do{
        vector<vector<int>> abt = reverse;
        int maxSum = 0;
        
        for(int i=0; i<sports; ++i){
            int maxAb = 0;
            int maxIdx = 0;
            for(int j=0; j<students; ++j){
                if(maxAb < abt[i][j]) {
                    maxAb = abt[i][j];
                    maxIdx = j;
                }
            }
            maxSum += maxAb;
            for(int k=0; k<sports; ++k){
                abt[k][maxIdx] = 0;
            }
        }
        if(answer < maxSum) answer = maxSum;
    }while(next_permutation(reverse.begin(), reverse.end()));
    
    return answer;
}

 

๋ชจ์˜๊ณ ์‚ฌ 1ํšŒ๋Š” 3๋ฒˆ ์ œ์™ธํ•˜๊ณ  ๋‹ค ํ•ด๊ฒฐํ–ˆ๋‹ค.

3๋ฒˆ์€.....์ˆ˜ํ•™ ๋ฌธ์ œ์ธ๋ฐ ๋ชป ํ’€๊ฒ ๋‹ค ใ… ใ…  ํžŒํŠธ ์–ป์–ด์•ผ ํ• ๋“ฏ

728x90
๋ฐ˜์‘ํ˜•