Skip to content

Commit 3b682c7

Browse files
committed
💯 [152] NeetCode's solution
1 parent cb66e05 commit 3b682c7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

152/o_solution.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const maxProduct = (nums) => {
6+
let min = 1, max = 1, result = Math.max(...nums);
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
let tmpMax = max;
10+
max = Math.max(nums[i] * min, nums[i] * max, nums[i]);
11+
min = Math.min(nums[i] * min, nums[i] * tmpMax, nums[i])
12+
result = Math.max(result, max)
13+
}
14+
15+
console.log(">")
16+
console.log(result)
17+
// console.log(max)
18+
// console.log(Math.max(result, max))
19+
20+
return result;
21+
};
22+
23+
maxProduct([2, 3, -2, 4])
24+
// maxProduct([-2, 0, -1])
25+
// maxProduct([0])
26+
// maxProduct([0, 2])
27+
// maxProduct([-2, 3, -4])
28+
// maxProduct([2, 3, -2, 4])
29+
// maxProduct([3, -1, 4]) // 4
30+
// maxProduct([2, -5, -2, -4, 3]) // 24
31+
// maxProduct([-2])
32+
// maxProduct([-1, -2, -9, -6])

0 commit comments

Comments
 (0)