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 8b3e245 commit 0eb48f9Copy full SHA for 0eb48f9
maximum-product-subarray/kimyoung.js
@@ -0,0 +1,23 @@
1
+var maxProduct = function (nums) { // brute force approach
2
+ let subarrays = [];
3
+ for (let i = 0; i < nums.length; i++) { // get subarrays
4
+ for (let j = i; j < nums.length; j++) {
5
+ subarrays.push(nums.slice(i, j + 1));
6
+ }
7
8
+
9
+ let maxProduct = 0;
10
+ subarrays.forEach(arr => { // find product of each subarray and compare to maxProduct
11
+ let prod = arr.reduce((acc, el) => acc *= el, 1);
12
+ max = Math.max(prod, max);
13
+ })
14
15
+ return maxProduct;
16
+};
17
18
+// time - O(n^2) double for loop
19
+// space - O(n^2) total count of subarrays = n*(n+1)/2
20
21
22
23
+// TODO - different approach
0 commit comments