Notice
Recent Posts
Recent Comments
Today
Total
01-11 01:45
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋” ๋งต๊ฒŒ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋” ๋งต๊ฒŒ

WonderJay 2022. 7. 11. 09:58
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

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

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

์Šค์ฝ”๋นŒ ์ง€์ˆ˜๊ฐ€ ๊ธฐ๋ก๋œ scoville ๋ฐฐ์—ด์—์„œ K ๋ณด๋‹ค ์ž‘์€ scoville ์„ ๊ฐ€์ง„ ์›์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ตœ์†Œ scoville ์™€, ๊ทธ ๋‹ค์Œ์œผ๋กœ ํฐ scoville ์„ ์ด์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ์ตœ์†Œํž™์„ ์‚ฌ์šฉํ•œ๋‹ค.

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

#define ll long long
using namespace std;

int solution(vector<int> scoville, int K) {
    priority_queue <int, vector<int>, greater<int> > pq;
    
    for(auto & ele : scoville)
    {
        pq.push(ele);
    }
    
    int cnt = 0; int mixed = 0; int first = 0; int second = 0;
    while(pq.size()!=1)
    {
        if(pq.top() >= K) break;
        first = pq.top();
        pq.pop();
        second = pq.top();
        pq.pop();
            
        mixed = first + second + second;
        pq.push(mixed);
        cnt++;
    }
    if(pq.top() < K) return -1;
    return cnt == 0 ? -1 : cnt;
}

 

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments