We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8a907ea commit d6fc961Copy full SHA for d6fc961
maximum-subarray/tolluset.ts
@@ -0,0 +1,35 @@
1
+function maxProduct(nums: number[]): number {
2
+ const n = nums.length;
3
+
4
+ if (n === 1) {
5
+ return nums[0];
6
+ }
7
8
+ let max = 0,
9
+ min = 0,
10
+ res = 0;
11
12
+ for (let i = 0; i < n; i++) {
13
+ const cur = nums[i];
14
15
+ if (cur < 0) {
16
+ [max, min] = [min, max];
17
18
19
+ max = Math.max(cur, max * cur);
20
+ min = Math.min(cur, min * cur);
21
22
+ res = Math.max(res, max);
23
24
25
+ return res;
26
+}
27
28
+const t1 = maxProduct([2, 3, -2, 4]);
29
+console.info("🚀 : tolluset.ts:3: t1=", t1); // 6
30
31
+const t2 = maxProduct([-2, 0, -1]);
32
+console.info("🚀 : tolluset.ts:6: t2=", t2); // 0
33
34
+const t3 = maxProduct([-2]);
35
+console.info("🚀 : tolluset.ts:34: t3=", t3); // -2
0 commit comments