Skip to content

Commit 51c0ad9

Browse files
committed
feat: add solutions to lc problem: No.1746
No.1746.Maximum Subarray Sum After One Operation
1 parent 2657506 commit 51c0ad9

File tree

13 files changed

+256
-55
lines changed

13 files changed

+256
-55
lines changed

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>
7777

7878
根据题目我们可以知道,函数 $f(x, y)$ 是单调递增函数,因此,我们可以枚举 $x$,然后在 $[1,...z]$ 中二分查找 $y$,使得 $f(x, y) = z$。如果找到了,就将 $(x, y)$ 加入答案中。
7979

80-
时间复杂度 $(z \log z)$,空间复杂度 $O(1)$。本题中 $z \le 100$。
80+
时间复杂度 $(n \log n)$,空间复杂度 $O(1)$。
8181

8282
**方法二:双指针**
8383

@@ -89,7 +89,7 @@ x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>
8989

9090
循环结束后,返回答案。
9191

92-
时间复杂度 $O(z)$,空间复杂度 $O(1)$。本题中 $z \le 100$。
92+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
9393

9494
<!-- tabs:start -->
9595

@@ -136,8 +136,8 @@ class Solution:
136136
class Solution:
137137
def findSolution(self, customfunction: "CustomFunction", z: int) -> List[List[int]]:
138138
ans = []
139-
x, y = 1, z
140-
while x <= z and y:
139+
x, y = 1, 1000
140+
while x <= 1000 and y:
141141
t = customfunction.f(x, y)
142142
if t < z:
143143
x += 1
@@ -168,8 +168,8 @@ class Solution:
168168
class Solution {
169169
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
170170
List<List<Integer>> ans = new ArrayList<>();
171-
for (int x = 1; x <= z; ++x) {
172-
int l = 1, r = z;
171+
for (int x = 1; x <= 1000; ++x) {
172+
int l = 1, r = 1000;
173173
while (l < r) {
174174
int mid = (l + r) >> 1;
175175
if (customfunction.f(x, mid) >= z) {
@@ -202,8 +202,8 @@ class Solution:
202202
class Solution {
203203
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
204204
List<List<Integer>> ans = new ArrayList<>();
205-
int x = 1, y = z;
206-
while (x <= z && y > 0) {
205+
int x = 1, y = 1000;
206+
while (x <= 1000 && y > 0) {
207207
int t = customfunction.f(x, y);
208208
if (t < z) {
209209
x++;
@@ -237,8 +237,8 @@ class Solution {
237237
public:
238238
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
239239
vector<vector<int>> ans;
240-
for (int x = 1; x <= z; ++x) {
241-
int l = 1, r = z;
240+
for (int x = 1; x <= 1000; ++x) {
241+
int l = 1, r = 1000;
242242
while (l < r) {
243243
int mid = (l + r) >> 1;
244244
if (customfunction.f(x, mid) >= z) {
@@ -273,8 +273,8 @@ class Solution {
273273
public:
274274
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
275275
vector<vector<int>> ans;
276-
int x = 1, y = z;
277-
while (x <= z && y) {
276+
int x = 1, y = 1000;
277+
while (x <= 1000 && y) {
278278
int t = customfunction.f(x, y);
279279
if (t < z) {
280280
x++;
@@ -302,8 +302,8 @@ public:
302302
*/
303303

304304
func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
305-
for x := 1; x <= z; x++ {
306-
y := 1 + sort.Search(z, func(y int) bool { return customFunction(x, y+1) >= z })
305+
for x := 1; x <= 1000; x++ {
306+
y := 1 + sort.Search(999, func(y int) bool { return customFunction(x, y+1) >= z })
307307
if customFunction(x, y) == z {
308308
ans = append(ans, []int{x, y})
309309
}
@@ -323,8 +323,8 @@ func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
323323
*/
324324

325325
func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
326-
x, y := 1, z
327-
for x <= z && y > 0 {
326+
x, y := 1, 1000
327+
for x <= 1000 && y > 0 {
328328
t := customFunction(x, y)
329329
if t < z {
330330
x++
@@ -352,9 +352,9 @@ func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
352352

353353
function findSolution(customfunction: CustomFunction, z: number): number[][] {
354354
const ans: number[][] = [];
355-
for (let x = 1; x <= z; ++x) {
355+
for (let x = 1; x <= 1000; ++x) {
356356
let l = 1;
357-
let r = z;
357+
let r = 1000;
358358
while (l < r) {
359359
const mid = (l + r) >> 1;
360360
if (customfunction.f(x, mid) >= z) {
@@ -382,9 +382,9 @@ function findSolution(customfunction: CustomFunction, z: number): number[][] {
382382

383383
function findSolution(customfunction: CustomFunction, z: number): number[][] {
384384
let x = 1;
385-
let y = z;
385+
let y = 1000;
386386
const ans: number[][] = [];
387-
while (x <= z && y) {
387+
while (x <= 1000 && y) {
388388
const t = customfunction.f(x, y);
389389
if (t < z) {
390390
++x;

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/README_EN.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ Binary search.
9191
class Solution:
9292
def findSolution(self, customfunction: "CustomFunction", z: int) -> List[List[int]]:
9393
ans = []
94-
for x in range(1, z + 1):
95-
y = 1 + bisect_left(range(1, z + 1), z, key=lambda y: customfunction.f(x, y))
94+
for x in range(1, 1001):
95+
y = 1 + bisect_left(range(1, 1001), z, key=lambda y: customfunction.f(x, y))
9696
if customfunction.f(x, y) == z:
9797
ans.append([x, y])
9898
return ans
@@ -114,8 +114,8 @@ class Solution:
114114
class Solution:
115115
def findSolution(self, customfunction: "CustomFunction", z: int) -> List[List[int]]:
116116
ans = []
117-
x, y = 1, z
118-
while x <= z and y:
117+
x, y = 1, 1000
118+
while x <= 1000 and y:
119119
t = customfunction.f(x, y)
120120
if t < z:
121121
x += 1
@@ -144,8 +144,8 @@ class Solution:
144144
class Solution {
145145
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
146146
List<List<Integer>> ans = new ArrayList<>();
147-
for (int x = 1; x <= z; ++x) {
148-
int l = 1, r = z;
147+
for (int x = 1; x <= 1000; ++x) {
148+
int l = 1, r = 1000;
149149
while (l < r) {
150150
int mid = (l + r) >> 1;
151151
if (customfunction.f(x, mid) >= z) {
@@ -178,8 +178,8 @@ class Solution:
178178
class Solution {
179179
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
180180
List<List<Integer>> ans = new ArrayList<>();
181-
int x = 1, y = z;
182-
while (x <= z && y > 0) {
181+
int x = 1, y = 1000;
182+
while (x <= 1000 && y > 0) {
183183
int t = customfunction.f(x, y);
184184
if (t < z) {
185185
x++;
@@ -213,8 +213,8 @@ class Solution {
213213
public:
214214
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
215215
vector<vector<int>> ans;
216-
for (int x = 1; x <= z; ++x) {
217-
int l = 1, r = z;
216+
for (int x = 1; x <= 1000; ++x) {
217+
int l = 1, r = 1000;
218218
while (l < r) {
219219
int mid = (l + r) >> 1;
220220
if (customfunction.f(x, mid) >= z) {
@@ -249,8 +249,8 @@ class Solution {
249249
public:
250250
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
251251
vector<vector<int>> ans;
252-
int x = 1, y = z;
253-
while (x <= z && y) {
252+
int x = 1, y = 1000;
253+
while (x <= 1000 && y) {
254254
int t = customfunction.f(x, y);
255255
if (t < z) {
256256
x++;
@@ -278,8 +278,8 @@ public:
278278
*/
279279

280280
func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
281-
for x := 1; x <= z; x++ {
282-
y := 1 + sort.Search(z, func(y int) bool { return customFunction(x, y+1) >= z })
281+
for x := 1; x <= 1000; x++ {
282+
y := 1 + sort.Search(999, func(y int) bool { return customFunction(x, y+1) >= z })
283283
if customFunction(x, y) == z {
284284
ans = append(ans, []int{x, y})
285285
}
@@ -299,8 +299,8 @@ func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
299299
*/
300300

301301
func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
302-
x, y := 1, z
303-
for x <= z && y > 0 {
302+
x, y := 1, 1000
303+
for x <= 1000 && y > 0 {
304304
t := customFunction(x, y)
305305
if t < z {
306306
x++
@@ -328,9 +328,9 @@ func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
328328

329329
function findSolution(customfunction: CustomFunction, z: number): number[][] {
330330
const ans: number[][] = [];
331-
for (let x = 1; x <= z; ++x) {
331+
for (let x = 1; x <= 1000; ++x) {
332332
let l = 1;
333-
let r = z;
333+
let r = 1000;
334334
while (l < r) {
335335
const mid = (l + r) >> 1;
336336
if (customfunction.f(x, mid) >= z) {
@@ -358,9 +358,9 @@ function findSolution(customfunction: CustomFunction, z: number): number[][] {
358358

359359
function findSolution(customfunction: CustomFunction, z: number): number[][] {
360360
let x = 1;
361-
let y = z;
361+
let y = 1000;
362362
const ans: number[][] = [];
363-
while (x <= z && y) {
363+
while (x <= 1000 && y) {
364364
const t = customfunction.f(x, y);
365365
if (t < z) {
366366
++x;

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Solution {
1414
public:
1515
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
1616
vector<vector<int>> ans;
17-
int x = 1, y = z;
18-
while (x <= z && y) {
17+
int x = 1, y = 1000;
18+
while (x <= 1000 && y) {
1919
int t = customfunction.f(x, y);
2020
if (t < z) {
2121
x++;

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
*/
99

1010
func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
11-
x, y := 1, z
12-
for x <= z && y > 0 {
11+
x, y := 1, 1000
12+
for x <= 1000 && y > 0 {
1313
t := customFunction(x, y)
1414
if t < z {
1515
x++

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
class Solution {
1313
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
1414
List<List<Integer>> ans = new ArrayList<>();
15-
int x = 1, y = z;
16-
while (x <= z && y > 0) {
15+
int x = 1, y = 1000;
16+
while (x <= 1000 && y > 0) {
1717
int t = customfunction.f(x, y);
1818
if (t < z) {
1919
x++;

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def f(self, x, y):
1313
class Solution:
1414
def findSolution(self, customfunction: "CustomFunction", z: int) -> List[List[int]]:
1515
ans = []
16-
x, y = 1, z
17-
while x <= z and y:
16+
x, y = 1, 1000
17+
while x <= 1000 and y:
1818
t = customfunction.f(x, y)
1919
if t < z:
2020
x += 1

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
function findSolution(customfunction: CustomFunction, z: number): number[][] {
1010
let x = 1;
11-
let y = z;
11+
let y = 1000;
1212
const ans: number[][] = [];
13-
while (x <= z && y) {
13+
while (x <= 1000 && y) {
1414
const t = customfunction.f(x, y);
1515
if (t < z) {
1616
++x;

solution/1700-1799/1746.Maximum Subarray Sum After One Operation/README.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,104 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
**方法一:动态规划**
43+
44+
我们定义 $f[i]$ 表示以 $nums[i]$ 结尾,且没有进行替换的最大子数组和,另外定义 $g[i]$ 表示以 $nums[i]$ 结尾,且进行了替换的最大子数组和。那么有如下状态转移方程:
45+
46+
$$
47+
\begin{aligned}
48+
f[i] &= \max(f[i - 1], 0) + nums[i] \\
49+
g[i] &= \max(\max(f[i - 1], 0) + nums[i] \times nums[i], g[i - 1] + nums[i])
50+
\end{aligned}
51+
$$
52+
53+
最终答案即为所有 $max(f[i], g[i])$ 的最大值。
54+
55+
由于 $f[i]$ 只与 $f[i - 1]$ 有关,因此我们可以只用两个变量来维护 $f[i]$ 和 $g[i]$ 的值,从而将空间复杂度降低到 $O(1)$。
56+
57+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $nums$ 的长度。
58+
4259
<!-- tabs:start -->
4360

4461
### **Python3**
4562

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

4865
```python
49-
66+
class Solution:
67+
def maxSumAfterOperation(self, nums: List[int]) -> int:
68+
f = g = 0
69+
ans = -inf
70+
for x in nums:
71+
ff = max(f, 0) + x
72+
gg = max(max(f, 0) + x * x, g + x)
73+
f, g = ff, gg
74+
ans = max(ans, f, g)
75+
return ans
5076
```
5177

5278
### **Java**
5379

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

5682
```java
83+
class Solution {
84+
public int maxSumAfterOperation(int[] nums) {
85+
int f = 0, g = 0;
86+
int ans = Integer.MIN_VALUE;
87+
for (int x : nums) {
88+
int ff = Math.max(f, 0) + x;
89+
int gg = Math.max(Math.max(f, 0) + x * x, g + x);
90+
f = ff;
91+
g = gg;
92+
ans = Math.max(ans, Math.max(f, g));
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int maxSumAfterOperation(vector<int>& nums) {
105+
int f = 0, g = 0;
106+
int ans = INT_MIN;
107+
for (int x : nums) {
108+
int ff = max(f, 0) + x;
109+
int gg = max(max(f, 0) + x * x, g + x);
110+
f = ff;
111+
g = gg;
112+
ans = max({ans, f, g});
113+
}
114+
return ans;
115+
}
116+
};
117+
```
57118
119+
### **Go**
120+
121+
```go
122+
func maxSumAfterOperation(nums []int) int {
123+
var f, g int
124+
ans := -(1 << 30)
125+
for _, x := range nums {
126+
ff := max(f, 0) + x
127+
gg := max(max(f, 0)+x*x, g+x)
128+
f, g = ff, gg
129+
ans = max(ans, max(f, g))
130+
}
131+
return ans
132+
}
133+
134+
func max(a, b int) int {
135+
if a > b {
136+
return a
137+
}
138+
return b
139+
}
58140
```
59141

60142
### **...**

0 commit comments

Comments
 (0)