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
This problem asks to find the maximum product of an array of non-negative integers after performing at most k
increment operations on its elements. The key insight is a greedy approach: it's always better to increment the smallest numbers. This is because the multiplicative effect of increasing a small number is greater than increasing a larger one.
Algorithm:
Use a Min-Heap: We utilize a min-heap data structure (priority queue) to efficiently track the smallest element in the array. Min-heaps allow us to quickly access and remove the minimum element.
Iterative Increments: We iterate k
times. In each iteration:
Calculate Product: After k
iterations, all increments are done. We then calculate the product of all elements in the min-heap, taking the modulo 10<sup>9</sup> + 7
to handle potential overflow.
Time Complexity:
nums
.k
iterations involves extracting the minimum (O(log n)), incrementing, and inserting back (O(log n)).Therefore, the overall time complexity is O(n + k log n). If k is significantly smaller than n, this approaches O(n). If k is much larger than n, it's dominated by O(k log n).
Space Complexity:
The space complexity is dominated by the min-heap, which stores the n
elements of the array. Therefore, the space complexity is O(n).
Code Examples (Python, Java, C++, Go, TypeScript, JavaScript):
The provided code snippets in multiple languages demonstrate this algorithm. They all follow the same structure:
Initialize a Min-Heap: Create a min-heap data structure and populate it with the elements of nums
.
Increment Iteratively: Perform k
iterations, extracting the minimum, incrementing, and re-inserting.
Calculate Product (with modulo): Calculate the product of all elements in the heap, ensuring modulo operation to prevent integer overflow.
Example Usage (Python):
from heapq import heapify, heapreplace
import operator
from functools import reduce
nums = [0,4]
k = 5
solution = Solution()
result = solution.maximumProduct(nums,k) #result = 20
nums = [6,3,3,2]
k = 2
result = solution.maximumProduct(nums,k) #result = 216
The other languages follow a similar pattern, utilizing their respective priority queue implementations (e.g., PriorityQueue
in Java, priority_queue
in C++, container/heap
in Go, MinPriorityQueue
in TypeScript and JavaScript). The core logic remains the same.