Created
June 12, 2022 15:07
-
-
Save hrit-ikkumar/a967eb801ff986d3ffa89a4f1b3b31af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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