Watch 10 video solutions for Maximal Score After Applying K Operations, a medium level problem involving Array, Greedy, Heap (Priority Queue). This walkthrough by NeetCodeIO has 7,408 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 0-indexed integer array nums and an integer k. You have a starting score of 0.
In one operation:
i such that 0 <= i < nums.length,nums[i], andnums[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.
Example 1:
Input: nums = [10,10,10,10,10], k = 5 Output: 50 Explanation: Apply the operation to each array element exactly once. The final score is 10 + 10 + 10 + 10 + 10 = 50.
Example 2:
Input: nums = [1,10,3,3,3], k = 3 Output: 17 Explanation: You can do the following operations: Operation 1: Select i = 1, so nums becomes [1,4,3,3,3]. Your score increases by 10. Operation 2: Select i = 1, so nums becomes [1,2,3,3,3]. Your score increases by 4. Operation 3: Select i = 2, so nums becomes [1,2,1,3,3]. Your score increases by 3. The final score is 10 + 4 + 3 = 17.
Constraints:
1 <= nums.length, k <= 1051 <= nums[i] <= 109Problem Overview: You are given an integer array nums and an integer k. For exactly k operations, choose the largest value in the array, add it to the score, then replace that value with ceil(value / 3). The goal is to maximize the total score after all operations.
Approach 1: Heap (Priority Queue) Method (Time: O((n + k) log n), Space: O(n))
This problem naturally fits a greedy strategy: always take the largest available number because it contributes the most to the score at that moment. A max heap makes this efficient. First push all elements of nums into a max heap. Then repeat k times: extract the largest value, add it to the running score, compute ceil(value / 3), and push the reduced value back into the heap. Each extraction and insertion costs O(log n), so the total work for k operations is O(k log n). The heap stores at most n elements, giving O(n) space usage. This approach handles large k efficiently and keeps the largest candidate accessible at all times. See related concepts in Heap (Priority Queue) and Greedy.
Approach 2: Sort and Greedy Method (Time: O(n log n + kn), Space: O(1) or O(n))
Another way is to keep the array sorted so the largest element is always at the end. Start by sorting nums in ascending order. For each of the k operations, take the largest element, add it to the score, replace it with ceil(value / 3), then reinsert the new value into the correct sorted position. Finding the position can be done with binary search in O(log n), but shifting elements to maintain sorted order costs O(n). Over k iterations, this leads to O(kn) time after the initial sort. Space complexity stays O(1) if sorting in place. This method works for moderate input sizes but becomes inefficient when k is large.
The greedy insight is straightforward: delaying the use of the largest value never increases the final score because every operation reduces the chosen value. Using the maximum immediately always yields the best marginal gain.
Recommended for interviews: The heap-based solution is the expected answer. It shows you recognize a repeated “extract max and update” pattern and apply a priority queue to keep operations efficient. Mentioning the sorted-array alternative demonstrates understanding of the greedy idea, but the heap implementation proves you can optimize it properly.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Heap (Priority Queue) | O((n + k) log n) | O(n) | Best general solution when repeatedly extracting and updating the maximum value |
| Sort and Greedy | O(n log n + kn) | O(1)–O(n) | Works for smaller inputs or when avoiding additional data structures |