알고리즘/리트코드

152. Maximum Product Subarray

창고 2021. 5. 23. 11:02

https://leetcode.com/problems/maximum-product-subarray/

 

Maximum Product Subarray - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

곱이 가장 큰 subarray 찾기

 

- 항상 현재 위치(nums[i])를 기준으로 preMin과 preMax를 갱신한다. 어차피 result가 최댓값을 기억한다.

- 작았던 값이 커질 수 있다.

 

Inputnums = [2,3,-2,4,-1];

Inputnums = [-2,0,1];

 

let preMin = Math.min(min * nums[i], nums[i], max * nums[i]);

let preMax = Math.max(min * nums[i], nums[i], max * nums[i]);

 

result = Math.max(result, preMax);