Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bf12709

Browse files
update: 152
1 parent 8eb2e03 commit bf12709

File tree

1 file changed

+25
-0
lines changed
  • src/maximum-product-subarray

1 file changed

+25
-0
lines changed

‎src/maximum-product-subarray/res.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,28 @@ let maxProduct = function(nums) {
3232

3333
return maxNow;
3434
};
35+
36+
let maxProduct_2 = (nums) => {
37+
let len = nums.length;
38+
if (!len) {
39+
return 0;
40+
}
41+
42+
let maxNow = nums[0];
43+
let imax = 1, imin = 1;
44+
for (let i = 0; i < len; i++) {
45+
if (nums[i] < 0) {
46+
let tmp = imax;
47+
imax = imin;
48+
imin = tmp;
49+
}
50+
51+
imax = Math.max(imax * nums[i], nums[i]),
52+
imin = Math.min(imin * nums[i], nums[i]);
53+
// console.log(imax, imin, nums[i])
54+
55+
maxNow = Math.max(maxNow, imax);
56+
}
57+
58+
return maxNow;
59+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /