2233. Maximum Product After K Increments LeetCode Solution
In this guide, you will get 2233. Maximum Product After K Increments LeetCode Solution with the best time and space complexity. The solution to Maximum Product After K Increments problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Product After K Increments solution in C++
- Maximum Product After K Increments solution in Java
- Maximum Product After K Increments solution in Python
- Additional Resources
Problem Statement of Maximum Product After K Increments
You are given an array of non-negative integers nums and an integer k. In one operation, you may choose any element from nums and increment it by 1.
Return the maximum product of nums after at most k operations. Since the answer may be very large, return it modulo 109 + 7. Note that you should maximize the product before taking the modulo.
Example 1:
Input: nums = [0,4], k = 5
Output: 20
Explanation: Increment the first number 5 times.
Now nums = [5, 4], with a product of 5 * 4 = 20.
It can be shown that 20 is maximum product possible, so we return 20.
Note that there may be other ways to increment nums to have the maximum product.
Example 2:
Input: nums = [6,3,3,2], k = 2
Output: 216
Explanation: Increment the second number 1 time and increment the fourth number 1 time.
Now nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216.
It can be shown that 216 is maximum product possible, so we return 216.
Note that there may be other ways to increment nums to have the maximum product.
Constraints:
1 <= nums.length, k <= 105
0 <= nums[i] <= 106
Complexity Analysis
- Time Complexity: O(k\log n + n\log n)
- Space Complexity: O(n)
2233. Maximum Product After K Increments LeetCode Solution in C++
class Solution {
public:
int maximumProduct(vector<int>& nums, int k) {
constexpr int kMod = 1'000'000'007;
long ans = 1;
priority_queue<int, vector<int>, greater<>> minHeap;
for (const int num : nums)
minHeap.push(num);
for (int i = 0; i < k; ++i) {
const int minNum = minHeap.top();
minHeap.pop();
minHeap.push(minNum + 1);
}
while (!minHeap.empty()) {
ans *= minHeap.top(), minHeap.pop();
ans %= kMod;
}
return ans;
}
};
/* code provided by PROGIEZ */
2233. Maximum Product After K Increments LeetCode Solution in Java
class Solution {
public int maximumProduct(int[] nums, int k) {
final int kMod = 1_000_000_007;
long ans = 1;
Queue<Integer> minHeap = new PriorityQueue<>();
for (final int num : nums)
minHeap.offer(num);
for (int i = 0; i < k; ++i) {
final int minNum = minHeap.poll();
minHeap.offer(minNum + 1);
}
while (!minHeap.isEmpty()) {
ans *= minHeap.poll();
ans %= kMod;
}
return (int) ans;
}
}
// code provided by PROGIEZ
2233. Maximum Product After K Increments LeetCode Solution in Python
class Solution:
def maximumProduct(self, nums: list[int], k: int) -> int:
kMod = 1_000_000_007
ans = 1
minHeap = nums.copy()
heapq.heapify(minHeap)
for _ in range(k):
minNum = heapq.heappop(minHeap)
heapq.heappush(minHeap, minNum + 1)
while minHeap:
ans *= heapq.heappop(minHeap)
ans %= kMod
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.