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 _themaximumproduct of _nums _afterat most _koperations. Since the answer may be very large, return it modulo109 + 7. Note that you should maximize the product before taking the modulo.
Input: nums =[0,4], k =5Output: 20Explanation: Increment the first number 5 times.Now nums =[5,4],with a product of 5*4=20.It can be shown that 20is maximum product possible, so we return20.Note that there may be other ways to increment nums to have the maximum product.
Input: nums =[6,3,3,2], k =2Output: 216Explanation: 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 216is maximum product possible, so we return216.Note that there may be other ways to increment nums to have the maximum product.
To maximize the product, we should always increment the smallest element in the array. By always increasing the minimum, we ensure the product grows as much as possible at each step. This is efficiently done using a min-heap (priority queue).
classSolution {
public:int maximumProduct(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int>> pq(nums.begin(), nums.end());
while (k--) {
int x = pq.top(); pq.pop();
pq.push(x +1);
}
longlong ans =1, mod =1e9+7;
while (!pq.empty()) {
ans = ans * pq.top() % mod;
pq.pop();
}
return ans;
}
};
import java.util.*;
classSolution {
publicintmaximumProduct(int[] nums, int k) {
PriorityQueue<Integer> pq =new PriorityQueue<>();
for (int x : nums) pq.offer(x);
while (k--> 0) {
pq.offer(pq.poll() + 1);
}
long ans = 1, mod = (long)1e9 + 7;
while (!pq.isEmpty()) {
ans = ans * pq.poll() % mod;
}
return (int)ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.PriorityQueue
classSolution {
funmaximumProduct(nums: IntArray, k: Int): Int {
val pq = PriorityQueue(nums.asList())
repeat(k) {
pq.add(pq.poll() + 1)
}
var ans = 1Lval mod = 1_000_000_007L
while (pq.isNotEmpty()) {
ans = ans * pq.poll() % mod
}
return ans.toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
import heapq
classSolution:
defmaximumProduct(self, nums: list[int], k: int) -> int:
heapq.heapify(nums)
for _ in range(k):
x = heapq.heappop(nums)
heapq.heappush(nums, x +1)
ans =1 mod =10**9+7for x in nums:
ans = ans * x % mod
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use std::collections::BinaryHeap;
use std::cmp::Reverse;
impl Solution {
pubfnmaximum_product(nums: Vec<i32>, k: i32) -> i32 {
letmut heap = nums.into_iter().map(Reverse).collect::<BinaryHeap<_>>();
letmut k = k;
while k >0 {
let Reverse(x) = heap.pop().unwrap();
heap.push(Reverse(x +1));
k -=1;
}
letmut ans =1i64;
let m =1_000_000_007i64;
whilelet Some(Reverse(x)) = heap.pop() {
ans = ans * x asi64% m;
}
ans asi32 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
maximumProduct(nums: number[], k: number):number {
constheap= [...nums];
heap.sort((a, b) =>a-b);
for (leti=0; i<k; ++i) {
heap[0]++;
letj=0;
while (j+1<heap.length&&heap[j] >heap[j+1]) {
[heap[j], heap[j+1]] = [heap[j+1], heap[j]];
j++;
}
}
letans=1, mod=1e9+7;
for (constxofheap) ans=ans*x%mod;
returnans;
}
}