Skip to content

Commit 8917900

Browse files
committed
solutions: 2530 - Maximal Score After Applying K Operations (Medium)
1 parent 2d244a1 commit 8917900

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

solutions/2500-2599/2530-maximal-score-after-applying-k-operations-medium.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
---
2-
description: 'Author: @wingkwong | https://leetcode.com/problems/maximal-score-after-applying-k-operations/'
2+
description: "Author: @wingkwong | https://leetcode.com/problems/maximal-score-after-applying-k-operations/"
3+
tags: [Array, Greedy, Heap (Priority Queue)]
34
---
45

5-
# 2530 - Maximal Score After Applying K Operations (Medium)
6+
# 2530 - Maximal Score After Applying K Operations (Medium)
67

78
## Problem Link
89

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

21-
Return *the maximum possible **score** you can attain after applying **exactly*** `k` *operations*.
22+
Return \*the maximum possible **score** you can attain after applying **exactly\*** `k` _operations_.
2223

2324
The ceiling function `ceil(val)` is the least integer greater than or equal to `val`.
2425

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

5051
## Approach 1: Priority Queue
5152

53+
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.
54+
55+
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.
56+
57+
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.
58+
5259
<Tabs>
5360
<TabItem value="cpp" label="C++">
5461
<SolutionAuthor name="@wingkwong"/>
@@ -63,11 +70,11 @@ public:
6370
// perform k rounds
6471
while (k--) {
6572
// get the max one
66-
int t = pq.top();
73+
int t = pq.top();
6774
// pop it out
6875
pq.pop();
6976
// add to answer
70-
ans += t;
77+
ans += t;
7178
// add the ceil value
7279
// ceil(x / y) = (x + y - 1) / y
7380
// ceil(t / 3) = (t + 3 - 1) / 3 = (t + 2) / 3
@@ -79,4 +86,32 @@ public:
7986
```
8087
8188
</TabItem>
82-
</Tabs>
89+
90+
<TabItem value="py" label="Python">
91+
<SolutionAuthor name="@wingkwong"/>
92+
93+
```py
94+
class Solution:
95+
def maxKelements(self, nums: List[int], k: int) -> int:
96+
res = 0
97+
# we want to take the max one in each round
98+
# create a max heap by pushing negative values
99+
mx_heap = [-num for num in nums]
100+
heapq.heapify(mx_heap)
101+
# perform k rounds
102+
while k > 0:
103+
k -= 1
104+
# get the max one (negative because of max-heap simulation)
105+
t = -heapq.heappop(mx_heap)
106+
# add to answer
107+
res += t
108+
# push the ceil value of t / 3 back into the heap
109+
# ceil(x / y) = (x + y - 1) / y
110+
# ceil(t / 3) = (t + 3 - 1) / 3 = (t + 2) / 3
111+
heapq.heappush(mx_heap, -((t + 2) // 3))
112+
return res
113+
```
114+
115+
</TabItem>
116+
117+
</Tabs>

0 commit comments

Comments
 (0)