You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 2530 - Maximal Score After Applying K Operations (Medium)
6
+
# 2530 - Maximal Score After Applying K Operations (Medium)
6
7
7
8
## Problem Link
8
9
@@ -18,7 +19,7 @@ In one **operation**:
18
19
2. increase your **score** by `nums[i]`, and
19
20
3. replace `nums[i]` with `ceil(nums[i] / 3)`.
20
21
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_.
22
23
23
24
The ceiling function `ceil(val)` is the least integer greater than or equal to `val`.
24
25
@@ -49,6 +50,12 @@ The final score is 10 + 4 + 3 = 17.
49
50
50
51
## Approach 1: Priority Queue
51
52
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.
0 commit comments