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 5441051 commit c4f616eCopy full SHA for c4f616e
maximum-product-subarray/soobing.ts
@@ -0,0 +1,22 @@
1
+/**
2
+ * 문제 설명
3
+ * - 배열에서 연속된 부분 배열의 곱이 가장 큰 값을 찾는 문제
4
+ *
5
+ * 아이디어
6
+ * - 1) 브루트포스 O(n^2)
7
+ * - 2) DP 최적화 O(n)
8
+ * - 매 index마다, 현재까지 max 곱, min 곱을 찾고 최대값을 갱신
9
+ */
10
+function maxProduct(nums: number[]): number {
11
+ let maxSoFar = nums[0];
12
+ let max = nums[0];
13
+ let min = nums[0];
14
+
15
+ for (let i = 1; i < nums.length; i++) {
16
+ const candidate = [nums[i], max * nums[i], min * nums[i]];
17
+ max = Math.max(...candidate);
18
+ min = Math.min(...candidate);
19
+ maxSoFar = Math.max(maxSoFar, max);
20
+ }
21
+ return maxSoFar;
22
+}
0 commit comments