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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ, ํž™(Heap)) C++

by UKHYUN22 2022. 1. 5.
728x90

 

 

priority_queue<int, vector<int>, greater<int>> => Min Heap
priority_queue<int, vector<int>, less<int>> => Max heap

 

์šฐ์„ ์ˆœ์œ„ ํ๋Š” ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ€์žฅ ํšจ์œจ์ ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋ฏ€๋กœ ํ•ด๋‹น STL์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์†์‰ฝ๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๋‘ ๊ฐœ์˜ Heap์œผ๋กœ ํ•˜๋‚˜์˜ ์ƒํ™ฉ์„ ์ฒ˜๋ฆฌํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ heap์˜ ํฌ๊ธฐ๋ฅผ ๋™์ผํ•˜๊ฒŒ ์นด์šดํŠธ ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ–ˆ๋‹ค๋Š” ์ ์ด๋‹ค. ๋งŒ์•ฝ pop์„ ํ–ˆ๋Š”๋ฐ ํฌ๊ธฐ๊ฐ€ 0์ด ๋œ ๊ฒฝ์šฐ ๋‘ ๊ฐœ์˜ Heap์„ ๋ชจ๋‘ ์ดˆ๊ธฐํ™”ํ•ด์ค„ ํ•„์š”๊ฐ€ ์žˆ์—ˆ๋‹ค. (์ œ์ผ ์ค‘์š”ํ•œ ์ )

 

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

using namespace std;

vector<int> solution(vector<string> operations) {
    vector<int> answer;
    int heap_size = 0;
    
    // Max ํž™๊ณผ Min ํž™์„ ์„ ์–ธํ•œ๋‹ค.
    priority_queue<int, vector<int>, greater<int>> min_heap;
    priority_queue<int, vector<int>, less<int>> max_heap;
    
    for(int i = 0 ; i < operations.size() ; i++) {
        
        // ๊ณต๋ฐฑ์„ ํฌํ•จํ•œ ๋ฌธ์ž๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ๋นŒ๋“œ์—…
        string str[3];
        string token;
        
        // stringstream์œผ๋กœ token์„ ๋งŒ๋“ค์–ด ์ •๋ฆฌํ•ด๋†“๋Š”๋‹ค.
        stringstream ss(operations[i]);
        int count = 0;
        
        // str ๋ฒกํ„ฐ ์•ˆ์— ์›ํ•˜๋Š” ๊ฐ’์„ ๋‹ด๋Š”๋‹ค.
        while(ss >> token) 
            str[count++] = token;
        
        // str[0] ์—๋Š” ๋ฌธ์ž str[1]์—๋Š” ์ถ”๊ฐ€ํ•  ์ˆซ์ž๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.
        int digit = stoi(str[1]);
        
        // I ์ธ ๊ฒฝ์šฐ max์™€ min Heap์— ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.
        if(str[0] == "I") {
            heap_size++;
            max_heap.push(digit);
            min_heap.push(digit);
        }
        
        // D์ธ ๊ฒฝ์šฐ pop์„ ์‹œํ–‰ํ•œ๋‹ค.
        else if(str[0] == "D" && heap_size != 0) {
            // pop์˜ ๊ฒฝ์šฐ heap์˜ ์ „์ฒด์ ์ธ ์‚ฌ์ด์ฆˆ๋ฅผ ํ•˜๋‚˜์”ฉ ์ค„์—ฌ์ค€๋‹ค.
            heap_size--;
            
            // ๋งŒ์•ฝ heap์„ ์ค„์˜€๋Š”๋ฐ ์‚ฌ์ด์ฆˆ๊ฐ€ 0์ด ๋˜์—ˆ์œผ๋ฉด ๋‹ค์‹œ ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์•ผํ•œ๋‹ค.
            if(heap_size == 0) {
                min_heap = priority_queue<int, vector<int>, greater<int>>();
                max_heap = priority_queue<int, vector<int>, less<int>>();
            }
            
            // digit์ด 1์ธ ๊ฒฝ์šฐ ์ตœ๋Œ€๊ฐ’์„ ์ œ๊ฑฐํ•˜๋ฏ€๋กœ max_heap์˜ pop์„ ํ•œ๋‹ค.
            if(digit == 1) {                
                if(!max_heap.empty()) {
                    max_heap.pop();
                }
            }
            
            // digit์ด -1์ธ ๊ฒฝ์šฐ ์ตœ์†Ÿ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋ฏ€๋กœ min_heap์˜ pop์„ ํ•œ๋‹ค.
            else if(digit == -1) {
                if(!min_heap.empty())
                    min_heap.pop();
            }
        }
    }
    
    // ํž™์˜ ํฌ๊ธฐ๊ฐ€ 0์ธ ๊ฒฝ์šฐ [0, 0] ์„ ๋„ฃ์–ด์ค€๋‹ค.
    if(heap_size == 0) {
        answer.push_back(0);
        answer.push_back(0);
    }
    
    // ์•„๋‹Œ ๊ฒฝ์šฐ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์˜ top์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
    else {
        answer.push_back(max_heap.top());
        answer.push_back(min_heap.top());
    }
    
    return answer;
}