Jesse and Cookies Hackerrank Solution Java | Data Structure
Note: Due to TLE i modified the some part of code.
Sample Input
STDIN Function ----- -------- 6 7 A[] size n = 6, k = 7 1 2 3 9 10 12 A = [1, 2, 3, 9, 10, 12]
Sample Output
2
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
static PriorityQueue<Integer> heap = new PriorityQueue<>();
/*
* Complete the cookies function below.
*/
static int cookies(int k) {
int count = 0;
while(heap.peek()<k && heap.size()>=2){
heap.add(heap.remove()+heap.remove()*2);
count++;
}
if(heap.size()==1 && heap.peek()<k){
count = -1;
}
return count;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int k = scn.nextInt();
for(int i=0;i<n;i++){
heap.add(scn.nextInt());
}
System.out.println(cookies(k));
}
}
Comments
Post a Comment