Skip to content

Instantly share code, notes, and snippets.

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 hrit-ikkumar/a967eb801ff986d3ffa89a4f1b3b31af to your computer and use it in GitHub Desktop.
Save hrit-ikkumar/a967eb801ff986d3ffa89a4f1b3b31af to your computer and use it in GitHub Desktop.
public class Solution {
public int solve(ArrayList<Integer> A, int B) {
// PriorityQueue for max heap
// By default priority queue is min heap
// we want to maximize profit that is reason we are using max heap
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
// add all the elements into the heap
for(int x: A) pq.add(x);
int ans = 0;
// select b elements out of heap
while(B-- > 0) {
int val = pq.poll();
ans += val;
// add remaining people to priority queue
pq.add(val - 1);
}
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment