Skip to content

Commit 6df9884

Browse files
committed
maximum product subarray solution
1 parent 9e1e31e commit 6df9884

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function maxProduct(nums: number[]): number {
2+
let minValue = nums[0];
3+
let maxValue = nums[0];
4+
let result = nums[0];
5+
6+
for (let i = 1; i < nums.length; i++) {
7+
const currentNum = nums[i];
8+
[minValue, maxValue] = [
9+
Math.min(currentNum, maxValue * currentNum, minValue * currentNum),
10+
Math.max(currentNum, maxValue * currentNum, minValue * currentNum),
11+
];
12+
13+
result = Math.max(result, maxValue);
14+
}
15+
16+
return result;
17+
}

0 commit comments

Comments
 (0)