-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathJesseAndCookies.java
31 lines (26 loc) Β· 967 Bytes
/
JesseAndCookies.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// https://www.hackerrank.com/challenges/jesse-and-cookies/problem
package heap;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class JesseAndCookies {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int threshold = scanner.nextInt();
Queue<Long> minHeap = new PriorityQueue<>();
while (length-- > 0) {
minHeap.add(scanner.nextLong());
}
System.out.println(minimumOperations(minHeap, threshold));
}
private static int minimumOperations(Queue<Long> minHeap, int threshold) {
int operations = 0;
while (minHeap.peek() < threshold && minHeap.size() >= 2) {
long cookie = minHeap.poll() + 2 * minHeap.poll();
minHeap.add(cookie);
operations++;
}
return minHeap.peek() >= threshold ? operations : -1 ;
}
}