Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created December 26, 2016 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/c34720a91e761163d73c9f1d90cdb065 to your computer and use it in GitHub Desktop.
Save jianminchen/c34720a91e761163d73c9f1d90cdb065 to your computer and use it in GitHub Desktop.
HackerRank - Jesse and Cookie - study Java code - https://www.hackerrank.com/challenges/jesse-and-cookies
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sweetness(sc);
}
public static void sweetness(Scanner sc){
int count = 0;
int N = sc.nextInt();
int K = sc.nextInt();
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
for(int i = 0; i < N; i++){
q.add(sc.nextInt());
}
while(q.peek() < K && q.size() >= 2){
q.add(q.remove() + 2 * q.remove());
count++;
}
if(q.size() == 1 && q.peek() < K){
count = -1;
}
System.out.println(count);
}
}
@rakeshsinha
Copy link

rakeshsinha commented Dec 19, 2020

You don't need to check for size at line 27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment