File tree Expand file tree Collapse file tree 2 files changed +22
-23
lines changed
Expand file tree Collapse file tree 2 files changed +22
-23
lines changed Original file line number Diff line number Diff line change 44
55### 성능 요약
66
7- 메모리: 129 MB, 시간: 1621.77 ms
7+ 메모리: 129 MB, 시간: 1664.78 ms
88
99### 구분
1010
1616
1717### 제출 일자
1818
19- 2025년 07월 20일 12:10:29
19+ 2025년 09월 02일 13:44:43
2020
2121### 문제 설명
2222
Original file line number Diff line number Diff line change 22
33class Solution {
44 public int solution (int [] scoville , int K ) {
5- PriorityQueue <Integer > scovilleHeap = new PriorityQueue <>();
5+ // 맵기를 기준으로 오름차순 정렬
6+ PriorityQueue <Integer > pq = new PriorityQueue <>((a ,b ) -> a - b );
67
7- for (int num : scoville ){
8- scovilleHeap .offer (num );
8+ for (int s : scoville ){
9+ pq .offer (s );
910 }
10-
11- int count = 0 ;
12-
13- while (scovilleHeap .size () > 1 ) {
14- // 모든 스코빌 지수가 K 이상이면
15- if (scovilleHeap .peek () >= K ) break ;
16-
17- // 맨 앞 두개 원소 꺼내서 계산
18- int newScoville = scovilleHeap .poll () + scovilleHeap .poll () * 2 ;
19-
20- // 새로운 인덱스 삽입
21- scovilleHeap .offer (newScoville );
22-
23- count ++;
11+
12+ int answer = 0 ;
13+ // 모든 음식이 K를 넘을때까지 반복
14+ while (pq .peek () < K && pq .size () > 1 ) {
15+ int k1 = pq .poll ();
16+ int k2 = pq .poll ();
17+
18+ int newK = k1 + (k2 * 2 );
19+ answer ++; // 횟수 추가
20+ pq .offer (newK ); // 새로 만든 맵기 추가
21+ }
22+
23+ if (pq .peek () < K ) { // 못만든 경우
24+ return -1 ;
25+ } else {
26+ return answer ;
2427 }
25-
26- if (scovilleHeap .peek () >= K ) return count ;
27- return -1 ;
2828 }
29-
3029}
You can’t perform that action at this time.
0 commit comments