Skip to content

Commit b90b376

Browse files
committed
feat: add solutions to lc problem: No.2233
No.2233.Maximum Product After K Increments
1 parent e3c003b commit b90b376

File tree

6 files changed

+207
-2
lines changed

6 files changed

+207
-2
lines changed

solution/2200-2299/2233.Maximum Product After K Increments/README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,97 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:贪心 + 优先队列(小根堆)**
49+
50+
每次操作,贪心地选择最小的元素进行加 $1$,共进行 $k$ 次操作。最后累乘所有元素得到结果。注意取模操作。
51+
52+
时间复杂度 $O(n+klogn)$。其中,$n$ 表示 $nums$ 的长度。建堆的时间复杂度为 $O(n)$,每次弹出最小元素进行加 $1$,再放回堆中,时间复杂度为 $O(logn)$,共进行 $k$ 次操作。
53+
4854
<!-- tabs:start -->
4955

5056
### **Python3**
5157

5258
<!-- 这里可写当前语言的特殊实现逻辑 -->
5359

5460
```python
55-
61+
class Solution:
62+
def maximumProduct(self, nums: List[int], k: int) -> int:
63+
heapify(nums)
64+
for _ in range(k):
65+
heappush(nums, heappop(nums) + 1)
66+
ans = 1
67+
mod = 10**9 + 7
68+
for v in nums:
69+
ans = (ans * v) % mod
70+
return ans
5671
```
5772

5873
### **Java**
5974

6075
<!-- 这里可写当前语言的特殊实现逻辑 -->
6176

6277
```java
78+
class Solution {
79+
private static final int MOD = (int) 1e9 + 7;
80+
81+
public int maximumProduct(int[] nums, int k) {
82+
PriorityQueue<Integer> q = new PriorityQueue<>();
83+
for (int v : nums) {
84+
q.offer(v);
85+
}
86+
while (k-- > 0) {
87+
q.offer(q.poll() + 1);
88+
}
89+
long ans = 1;
90+
while (!q.isEmpty()) {
91+
ans = (ans * q.poll()) % MOD;
92+
}
93+
return (int) ans;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int maximumProduct(vector<int>& nums, int k) {
104+
int mod = 1e9 + 7;
105+
make_heap(nums.begin(), nums.end(), greater<int>());
106+
while (k--)
107+
{
108+
pop_heap(nums.begin(), nums.end(), greater<int>());
109+
++nums.back();
110+
push_heap(nums.begin(), nums.end(), greater<int>());
111+
}
112+
long long ans = 1;
113+
for (int v : nums) ans = (ans * v) % mod;
114+
return ans;
115+
}
116+
};
117+
```
63118
119+
### **Go**
120+
121+
```go
122+
func maximumProduct(nums []int, k int) int {
123+
h := hp{nums}
124+
for heap.Init(&h); k > 0; k-- {
125+
h.IntSlice[0]++
126+
heap.Fix(&h, 0)
127+
}
128+
ans := 1
129+
for _, v := range nums {
130+
ans = (ans * v) % (1e9 + 7)
131+
}
132+
return ans
133+
}
134+
135+
type hp struct{ sort.IntSlice }
136+
137+
func (hp) Push(interface{}) {}
138+
func (hp) Pop() (_ interface{}) { return }
64139
```
65140

66141
### **JavaScript**

solution/2200-2299/2233.Maximum Product After K Increments/README_EN.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,82 @@ Note that there may be other ways to increment nums to have the maximum product.
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def maximumProduct(self, nums: List[int], k: int) -> int:
51+
heapify(nums)
52+
for _ in range(k):
53+
heappush(nums, heappop(nums) + 1)
54+
ans = 1
55+
mod = 10**9 + 7
56+
for v in nums:
57+
ans = (ans * v) % mod
58+
return ans
5059
```
5160

5261
### **Java**
5362

5463
```java
64+
class Solution {
65+
private static final int MOD = (int) 1e9 + 7;
66+
67+
public int maximumProduct(int[] nums, int k) {
68+
PriorityQueue<Integer> q = new PriorityQueue<>();
69+
for (int v : nums) {
70+
q.offer(v);
71+
}
72+
while (k-- > 0) {
73+
q.offer(q.poll() + 1);
74+
}
75+
long ans = 1;
76+
while (!q.isEmpty()) {
77+
ans = (ans * q.poll()) % MOD;
78+
}
79+
return (int) ans;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
int maximumProduct(vector<int>& nums, int k) {
90+
int mod = 1e9 + 7;
91+
make_heap(nums.begin(), nums.end(), greater<int>());
92+
while (k--)
93+
{
94+
pop_heap(nums.begin(), nums.end(), greater<int>());
95+
++nums.back();
96+
push_heap(nums.begin(), nums.end(), greater<int>());
97+
}
98+
long long ans = 1;
99+
for (int v : nums) ans = (ans * v) % mod;
100+
return ans;
101+
}
102+
};
103+
```
55104
105+
### **Go**
106+
107+
```go
108+
func maximumProduct(nums []int, k int) int {
109+
h := hp{nums}
110+
for heap.Init(&h); k > 0; k-- {
111+
h.IntSlice[0]++
112+
heap.Fix(&h, 0)
113+
}
114+
ans := 1
115+
for _, v := range nums {
116+
ans = (ans * v) % (1e9 + 7)
117+
}
118+
return ans
119+
}
120+
121+
type hp struct{ sort.IntSlice }
122+
123+
func (hp) Push(interface{}) {}
124+
func (hp) Pop() (_ interface{}) { return }
56125
```
57126

58127
### **JavaScript**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int maximumProduct(vector<int>& nums, int k) {
4+
int mod = 1e9 + 7;
5+
make_heap(nums.begin(), nums.end(), greater<int>());
6+
while (k--)
7+
{
8+
pop_heap(nums.begin(), nums.end(), greater<int>());
9+
++nums.back();
10+
push_heap(nums.begin(), nums.end(), greater<int>());
11+
}
12+
long long ans = 1;
13+
for (int v : nums) ans = (ans * v) % mod;
14+
return ans;
15+
}
16+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func maximumProduct(nums []int, k int) int {
2+
h := hp{nums}
3+
for heap.Init(&h); k > 0; k-- {
4+
h.IntSlice[0]++
5+
heap.Fix(&h, 0)
6+
}
7+
ans := 1
8+
for _, v := range nums {
9+
ans = (ans * v) % (1e9 + 7)
10+
}
11+
return ans
12+
}
13+
14+
type hp struct{ sort.IntSlice }
15+
16+
func (hp) Push(interface{}) {}
17+
func (hp) Pop() (_ interface{}) { return }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
private static final int MOD = (int) 1e9 + 7;
3+
4+
public int maximumProduct(int[] nums, int k) {
5+
PriorityQueue<Integer> q = new PriorityQueue<>();
6+
for (int v : nums) {
7+
q.offer(v);
8+
}
9+
while (k-- > 0) {
10+
q.offer(q.poll() + 1);
11+
}
12+
long ans = 1;
13+
while (!q.isEmpty()) {
14+
ans = (ans * q.poll()) % MOD;
15+
}
16+
return (int) ans;
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maximumProduct(self, nums: List[int], k: int) -> int:
3+
heapify(nums)
4+
for _ in range(k):
5+
heappush(nums, heappop(nums) + 1)
6+
ans = 1
7+
mod = 10**9 + 7
8+
for v in nums:
9+
ans = (ans * v) % mod
10+
return ans

0 commit comments

Comments
 (0)