Search

더 맵게

알고리즘
힙(Heap)
플랫폼
프로그래머스
JCF
PriorityQueue
상태
해결
생성 일시
2024/01/07 09:41
최종 편집 일시
2024/01/07 11:30

문제 설명

해결과정

Solution.java

import java.util.*; class Solution { public int solution(int[] scoville, int K) { int answer = 0; // scoville LinkedList PriorityQueue<Integer> queue = new PriorityQueue<>(); // scoville배열 LinkedList에 넣기 for(int hot : scoville) { queue.add(hot); } // LinkedList 정렬 while(true) { // 가장 맵지 않은 음식의 스코빌 지수 int first = queue.remove(); // 가장 맵지 않은 음식의 스코빌 지수가 K이상일 경우 break; if(first >= K) { break; } // 두번째로 맵지 않은 음식의 스코빌 지수 int second = queue.remove(); int third = first + (second * 2); queue.add(third); if(queue.size() == 1 && third < K) { answer = -1; break; } answer++; } return answer; } } // 1. scovile 배열을 LinkedList로 변경 // 2. LinkedList 정렬 // 3. 가장 앞에 있는 값 가져오기 // 4. 그 다음 값 가져와서 더하기 // 5. 더한 값 LinkedList 에 넣기 // 6. 이 과정을 가장 앞에 있는 값이 K가 될때까지 반복
Java
복사