Skip to content

Commit abd23dd

Browse files
committed
[level 2] Title: 더 맵게, Time: 1664.78 ms, Memory: 129 MB -BaekjoonHub
1 parent 20dc085 commit abd23dd

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

프로그래머스/2/42626. 더 맵게/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 129 MB, 시간: 1621.77 ms
7+
메모리: 129 MB, 시간: 1664.78 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 07월 20일 12:10:29
19+
2025년 09월 02일 13:44:43
2020

2121
### 문제 설명
2222

프로그래머스/2/42626. 더 맵게/더 맵게.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@
22

33
class 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
}

0 commit comments

Comments
 (0)