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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

[1]. PriorityHeapQueue 를 구현하여 스코빌지수가 작은순으로 PriorityHeapQueue를 구현합니다.
    (기본적으로 PrirotiyQueue는 최소힙입니다. 
    만약 최대힙으로 구현하고 싶다면 PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder()); 를 사용합니다. )
[2]. 스코빌 지수배열을 다 넣습니다.
[3]. 우선순위큐의 최소힙값이 K보다 크다면, 안의 내용물이 모두 K 보다 크다는 뜻이므로 그 조건으로 반복문을 돌립니다.
    - 우선순위큐에서 첫번째와 두번째를 빼서 섞은 음식의 스코빌 지수 = 가장 맵지 않은 음식의 스코빌 지수 + (두 번째로 맵지 않은 음식의 스코빌 지수 * 2) 공식을 사용한뒤
    - pq에 다시 넣습니다.
    - 그리고 그중에서 pq.size()가 2보다 작다면 불가능하므로 조건식으로 flag를 처리합니다.

 

 

 

 

import java.util.*;
class Solution {
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
public int solution(int[] scoville, int K) {
int answer = 0;
boolean impossible = false;
for(int i=0;i<scoville.length;i++){
pq.offer(scoville[i]);
}
while(pq.peek() < K){
if(pq.size() < 2 ){ impossible = true; break; }
int first = pq.poll();
int second = pq.poll();
int sum = first + second * 2;
pq.offer(sum);
answer += 1;
}
if(impossible == true)
return -1;
return answer;
}
}

+ Recent posts