-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create MaximalScoreAfterApplyingKOperations.java
- Loading branch information
1 parent
cd1b4c1
commit 3eb3a1a
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
2530. Maximal Score After Applying K Operations | ||
Solved | ||
Medium | ||
Topics | ||
Companies | ||
Hint | ||
You are given a 0-indexed integer array nums and an integer k. You have a starting score of 0. | ||
In one operation: | ||
choose an index i such that 0 <= i < nums.length, | ||
increase your score by nums[i], and | ||
replace nums[i] with ceil(nums[i] / 3). | ||
Return the maximum possible score you can attain after applying exactly k operations. | ||
The ceiling function ceil(val) is the least integer greater than or equal to val. | ||
**/ | ||
class Solution { | ||
public long maxKelements(int[] nums, int k) { | ||
PriorityQueue<Integer>pq = new PriorityQueue<>((a, b)->b-a); | ||
for(int x : nums) | ||
pq.offer(x); | ||
long score = 0; | ||
while(!pq.isEmpty() && k>0) | ||
{ | ||
score +=pq.peek(); | ||
int x = (int)Math.ceil(pq.poll()/3.0); | ||
pq.add(x); | ||
k--; | ||
} | ||
return score; | ||
} | ||
} |