Skip to content

Commit 4f2f6fb

Browse files
authored
fix: JS代码位置不对
1 parent 2aecd18 commit 4f2f6fb

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

problems/152.maximum-product-subarray.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ var maxProduct = function(nums) {
5656
## 关键点
5757

5858
- 同时记录乘积最大值和乘积最小值
59-
6059
## 代码
6160

6261
代码支持:Python3,JavaScript
@@ -86,27 +85,15 @@ class Solution:
8685
```
8786

8887

89-
JavaScript Code:
88+
**复杂度分析**
89+
- 时间复杂度:$O(N)$
90+
- 空间复杂度:$O(N)$
9091

91-
```js
92-
var maxProduct = function(nums) {
93-
let max = nums[0];
94-
let min = nums[0];
95-
let res = nums[0];
9692

97-
for (let i = 1; i < nums.length; i++) {
98-
let tmp = min;
99-
min = Math.min(nums[i], Math.min(max * nums[i], min * nums[i])); // 取最小
100-
max = Math.max(nums[i], Math.max(max * nums[i], tmp * nums[i])); /// 取最大
101-
res = Math.max(res, max);
102-
}
103-
return res;
104-
};
105-
```
10693
当我们知道动态转移方程的时候,其实应该发现了。我们的dp[i] 只和 dp[i - 1]有关,这是一个空间优化的信号,告诉我们`可以借助两个额外变量记录即可`
10794

10895

109-
代码(Python3):
96+
Python3 Code:
11097

11198

11299
```python
@@ -127,3 +114,26 @@ class Solution:
127114
return ans
128115

129116
```
117+
118+
JavaScript Code:
119+
120+
```js
121+
var maxProduct = function(nums) {
122+
let max = nums[0];
123+
let min = nums[0];
124+
let res = nums[0];
125+
126+
for (let i = 1; i < nums.length; i++) {
127+
let tmp = min;
128+
min = Math.min(nums[i], Math.min(max * nums[i], min * nums[i])); // 取最小
129+
max = Math.max(nums[i], Math.max(max * nums[i], tmp * nums[i])); /// 取最大
130+
res = Math.max(res, max);
131+
}
132+
return res;
133+
};
134+
```
135+
136+
137+
**复杂度分析**
138+
- 时间复杂度:$O(N)$
139+
- 空间复杂度:$O(1)$

0 commit comments

Comments
 (0)