Skip to content

Commit

Permalink
Create MaximalScoreAfterApplyingKOperations.java
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 authored Oct 16, 2024
1 parent cd1b4c1 commit 3eb3a1a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Leetcode/MaximalScoreAfterApplyingKOperations.java
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;
}
}

0 comments on commit 3eb3a1a

Please sign in to comment.