Problem 2530 Maximal Score After Applying K Operations
Table of Contents
Problem Statement
Link - Problem 2530
Question
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 that0 <= i < nums.length
, - increase your score by
nums[i]
, and - replace
nums[i]
withceil(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 <= 105
1 <= nums[i] <= 109
Solution
class Solution {
public:
static long long maxKelements(vector<int>& nums, int k) {
priority_queue<int> pq(nums.begin(), nums.end());
long long score=0;
for(int i=0; i<k; i++){
int x=pq.top();
score+=x;
if (x==1){// early stop
score+=(k-1-i);
break;
}
pq.pop();
pq.push((x+2)/3);
}
return score;
}
};
Complexity Analysis
| Algorithm | Time Complexity | Space Complexity |
| -------------- | --------------- | ---------------- |
| Priority Queue | O(n log n) | O(n) |
Explanation
Intial Thoughts
To approach this problem, we should first identify the key elements: the given array of integers, the number of operations, and the goal of maximizing the score. We should consider how the score changes after each operation and think about strategies to optimize it. Some initial ideas include prioritizing the largest numbers in the array, considering the impact of the ceiling function on the numbers, and exploring ways to maximize the overall score after multiple operations. We should also think about how to efficiently select the next number to operate on. Additionally, we should consider the constraints on the input values and the number of operations.
Intuitive Analysis
Intuitively, we want to focus on the largest numbers in the array first, as they will give us the most significant increase in score. Using a priority queue can help us keep track of the largest numbers efficiently. We should repeatedly select the largest number, add it to the score, and then update the number using the given formula. It’s essential to consider the point at which operating on a number no longer provides a significant increase in score, such as when the number becomes 1. At that point, we can use the remaining operations to add the minimum possible score. By following this greedy approach, we can intuitively solve the problem and find the maximum possible score.
1. Intuition
- To maximize the score, we need to choose the largest numbers from the array
nums
in each operation - Using a priority queue
pq
allows us to efficiently select the maximum number in each operation - The key insight is to replace the chosen number with its ceiling divided by 3, which is implemented as
(x+2)/3
- This process is repeated
k
times to achieve the maximum possible score - If the chosen number becomes 1, we can stop the process early because further operations will not increase the score
- The algorithmic choice of using a priority queue ensures that the solution works efficiently for large inputs
- The time complexity of the solution is O(k log n) due to the priority queue operations
2. Implementation
- The
maxKelements
function takes a vectornums
and an integerk
as input and returns the maximum possible score - A priority queue
pq
is initialized with the elements ofnums
usingpq(nums.begin(), nums.end())
- The score is initialized to 0 and incremented by the maximum number
x
in each operation usingscore += x
- The chosen number
x
is replaced with its ceiling divided by 3 usingpq.push((x+2)/3)
- The process is repeated
k
times using a for loopfor(int i=0; i<k; i++)
- If the chosen number becomes 1, the process is stopped early using
if (x==1)
and the remaining operations are accounted for usingscore+=(k-1-i)
- The final score is returned as a long long integer using
return score
Complexity Analysis
Time Complexity:
- The time complexity of the given algorithm is dominated by two main operations: the construction of the
priority_queue
and the loop that pops and pushes elements from and to the queue. The construction of thepriority_queue
takes O(n log n) time because it involves heapifying the input array of size n. - The loop that runs k times, where each iteration involves
pq.top()
,pq.pop()
, andpq.push((x+2)/3)
, takes O(k log n) time in the worst case because each of these operations takes O(log n) time in a priority queue. - Since the problem statement doesn’t provide any relation between n and k, we consider the worst-case scenario where k can be as large as n, resulting in a time complexity of O(n log n) due to the queue operations.
Space Complexity:
- The space complexity of the algorithm is primarily determined by the space required to store the input array and the
priority_queue
. - The input array
nums
requires O(n) space because it contains n elements, and thepriority_queue
also requires O(n) space in the worst case because it can contain up to n elements. - Therefore, the overall space complexity of the algorithm is O(n) due to the storage of the input array and the priority queue.
Footnote
This question is rated as Medium difficulty.
Hints
It is always optimal to select the greatest element in the array.
Use a heap to query for the maximum in O(log n) time.
Similar Questions:
Title | URL | Difficulty |
---|---|---|
Sliding Window Maximum | https://leetcode.com/problems/sliding-window-maximum | Hard |
Remove Stones to Minimize the Total | https://leetcode.com/problems/remove-stones-to-minimize-the-total | Medium |