본문 바로가기
프로그래머스

프로그래머스 이중우선순위큐

by 콩순이냉장고 2021. 7. 22.

문제 URL : https://programmers.co.kr/learn/courses/30/lessons/42628

 

코딩테스트 연습 - 이중우선순위큐

 

programmers.co.kr

 

문제접근법 : map을 이용하여 풀면 최솟값 최대값을 쉽게 찾을수있는 문제입니다.

백준에 이중우선순위큐와 똑같은 문제니 https://congsoony.tistory.com/104

 

백준 7662 이중 우선순위 큐

문제 URL :https://www.acmicpc.net/problem/7662 7662번: 이중 우선순위 큐 입력 데이터는 표준입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내

congsoony.tistory.com

제 글을 참조하여 풀이법이 거의 동일합니다.

 

소스코드 : 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <bits/stdc++.h>
using namespace std;
vector<string> split(string& s, char delimiter = ' ') {
    stringstream ss(s);
    string temp;
    vector<string> v;
    while (getline(ss, temp, delimiter))
        v.push_back(temp);
    return v;
}
vector<int> solution(vector<string> operations) {
    vector<int> answer;
    map<intint> m;
    for (string& s : operations) {
        vector<string> v = split(s);
        if (v[0== "D") {
            if (m.empty())continue;
            int t = (v[1== "1" ? (--m.end())->first : m.begin()->first);
            if (--m[t] <= 0)
                m.erase(t);
        }
        else {
            int num = stoi(v[1]);
            m[num]++;
        }
    }
    if (m.empty())return { 0,0 };
    return { (--m.end())->first , m.begin()->first };
}
cs

궁금한점 혹은 모르느점 어떤질문이든 댓글은 언제나 환영입니다.