Skip to content

Commit

Permalink
solutions: 2530 - Maximal Score After Applying K Operations (Medium)
Browse files Browse the repository at this point in the history
  • Loading branch information
wingkwong committed Oct 14, 2024
1 parent 2d244a1 commit 8917900
Showing 1 changed file with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
description: 'Author: @wingkwong | https://leetcode.com/problems/maximal-score-after-applying-k-operations/'
description: "Author: @wingkwong | https://leetcode.com/problems/maximal-score-after-applying-k-operations/"
tags: [Array, Greedy, Heap (Priority Queue)]
---

# 2530 - Maximal Score After Applying K Operations (Medium)
# 2530 - Maximal Score After Applying K Operations (Medium)

## Problem Link

Expand All @@ -18,7 +19,7 @@ In one **operation**:
2. increase your **score** by `nums[i]`, and
3. replace `nums[i]` with `ceil(nums[i] / 3)`.

Return *the maximum possible **score** you can attain after applying **exactly*** `k` *operations*.
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`.

Expand Down Expand Up @@ -49,6 +50,12 @@ The final score is 10 + 4 + 3 = 17.

## Approach 1: Priority Queue

When you see a question asking you to find something maximum / minimum after `K` operations. It's a hint that this could be solved by priority queue.

In this question, it is easy to see that we should apply the operation on the largest number each time (i.e. greedy approach). If we just write a brute force solution, after each operation, we need to iterate `nums` again to find out the largest number which costs $O(n)$ where $n$ is the number of elements in `nums` and this happens $K$ times, which is not efficient.

To optimise the solution, we can apply priority queue / heap (in python) so that we could find the largest number efficiently, which takes $O(1)$ for finding the max number.

<Tabs>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@wingkwong"/>
Expand All @@ -63,11 +70,11 @@ public:
// perform k rounds
while (k--) {
// get the max one
int t = pq.top();
int t = pq.top();
// pop it out
pq.pop();
// add to answer
ans += t;
ans += t;
// add the ceil value
// ceil(x / y) = (x + y - 1) / y
// ceil(t / 3) = (t + 3 - 1) / 3 = (t + 2) / 3
Expand All @@ -79,4 +86,32 @@ public:
```
</TabItem>
</Tabs>
<TabItem value="py" label="Python">
<SolutionAuthor name="@wingkwong"/>
```py
class Solution:
def maxKelements(self, nums: List[int], k: int) -> int:
res = 0
# we want to take the max one in each round
# create a max heap by pushing negative values
mx_heap = [-num for num in nums]
heapq.heapify(mx_heap)
# perform k rounds
while k > 0:
k -= 1
# get the max one (negative because of max-heap simulation)
t = -heapq.heappop(mx_heap)
# add to answer
res += t
# push the ceil value of t / 3 back into the heap
# ceil(x / y) = (x + y - 1) / y
# ceil(t / 3) = (t + 3 - 1) / 3 = (t + 2) / 3
heapq.heappush(mx_heap, -((t + 2) // 3))
return res
```

</TabItem>

</Tabs>

0 comments on commit 8917900

Please sign in to comment.