๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš“ Self Study/๐Ÿ”“ Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (๋””์Šคํฌ ์ปจํŠธ๋กค๋Ÿฌ, ํž™(Heap)) C++

by UKHYUN22 2022. 1. 5.
728x90

 

sort ์ •๋ ฌ์„ ํ•  ๋•Œ cmp๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ž‘์„ฑํ–ˆ์—ˆ์ง€๋งŒ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šฌ ์ „๋ถ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. ์š”์ฒญํ•œ ์ˆœ์„œ๋Œ€๋กœ ์ถ”๊ฐ€๋ฅผ ํ•˜๊ณ  ์ถ”๊ฐ€๋ฅผ ํ•  ๋•Œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•ด์„œ operation์„ ์ถ”๊ฐ€ํ•ด์„œ ๋„ฃ์œผ๋ฉด ํ…Œ์ŠคํŠธ ์ผ€์ด์Šฌ ๋ชจ๋‘ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค.

 

#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <iostream>

using namespace std;

struct cmp {
    bool operator()(vector<int>a, vector<int> b) {
        return a[1] > b[1];
    }
};

int solution(vector<vector<int>> jobs) {
    int answer = 0;
    int task = 0;
    int time = 0;
    
    // pq์— ๊ฐ’์„ ๋„ฃ๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ ์ž‘์—… ์‹œ๊ฐ„์ด ์งง์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ์ด ๋œ๋‹ค.
    priority_queue<vector<int>, vector<vector<int>>, cmp> pq;
    
    // ์š”์ฒญ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ
    sort(jobs.begin(), jobs.end());
    
    while(task < jobs.size() || !pq.empty()) {
        if(jobs.size() > task && time >= jobs[task][0]) {
            pq.push(jobs[task++]);
            continue;
        }
        
        if(!pq.empty()) {
            time += pq.top()[1];
            answer += time - pq.top()[0];
            pq.pop();
        } else
            time = jobs[task][0];
    }
    
    return answer/jobs.size();
}

 

 

๋‹ค์Œ์€ ํ•ด๋‹น ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ชจ๋‘ ํ†ต๊ณผํ•œ ์ฝ”๋“œ

 

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

int solution(vector<vector<int>> jobs) {
    int answer = 0;
    int time = 0;
    int sum = 0;
    
    for(int i = 0 ; i < jobs.size() ; i++) {
        int count = 0;
        int index = 0;
        
        time += jobs[i][1];
        int next = jobs[i+1][1];
        
        // ํ˜„์žฌ ์‹œํ–‰ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ ๋‹ค์Œ๋ถ€ํ„ฐ for๋ฌธ์œผ๋กœ ๋Œ๋ฆฐ๋‹ค.
        for(int j = i+1 ; j < jobs.size() ; j++) {
            
            // ๋งŒ์•ฝ ์ด์ „ ์‹œํ–‰ํ•œ ์‹œ๊ฐ„ ๋‚ด์— ์š”์ฒญ์ด ๋“ค์–ด์˜จ ์ž‘์—…์ด ์žˆ๋Š” ๊ฒฝ์šฐ
            if(jobs[j][0] <= time) {
                
                // ๋‹ค์Œ ์‹œํ–‰ํ•  ๊ฒƒ์˜ ์ž‘์—… ์‹œ๊ฐ„์ด next๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ
                // next๋ฅผ ์ตœ์‹ ํ™”ํ•˜๊ณ  index ๋ณ€์ˆ˜์— ์›ํ•˜๋Š” index ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.
                if(next > jobs[j][1]) {
                    count++;
                    next = jobs[j][1];
                    index = j;
                }
            }
        }
        
        // ๋งŒ์•ฝ count๋œ ๊ฒƒ์ด ํ•˜๋‚˜๋ผ๋„ ์กด์žฌํ•˜๋ฉด ์ž‘์—… ์‹œ๊ฐ„ ๋‚ด์— ์š”์ฒญ์ด ๋“ค์–ด์˜จ ๊ฒƒ์ด
        // 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ์ตœ์†Œ์ž‘์—… ์‹œ๊ฐ„์„ ๊ฐ–๋Š” ์š”์ฒญ์„ i+1 ๋ฒˆ์งธ์™€ ๋ฐ”๊ฟ”์ค€๋‹ค.
        if(count >= 1) {
            vector<int> temp = jobs[index];
            jobs[index] = jobs[i+1];
            jobs[i+1] = temp;
        }
    }
    
    for(int i= 0 ; i < jobs.size() ; i++) {
        cout << jobs[i][0] << " " << jobs[i][1] << endl;
    }
    
    for(int i= 0 ; i < jobs.size() ; i++) {
        sum += jobs[i][1];
        answer += sum - jobs[i][0];
    }
    
    answer = answer / jobs.size();
    
    return answer;
}