MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / maxProduct

Function maxProduct

JavaScript/152.Maximum-Product-Subarray.js:20–37  ·  view source on GitHub ↗
(nums)

Source from the content-addressed store, hash-verified

18 * @return {number}
19 */
20var maxProduct = function (nums) {
21 if (!nums.length) return 0;
22 let maxEnding = nums[0];
23 let minEnding = nums[0];
24 let res = nums[0];
25 for (let i = 1; i < nums.length; i++) {
26 if (nums[i] < 0) {
27 let temp = minEnding;
28 minEnding = maxEnding;
29 maxEnding = temp;
30 }
31
32 maxEnding = Math.max(maxEnding * nums[i], nums[i]);
33 minEnding = Math.min(minEnding * nums[i], nums[i]);
34 res = Math.max(res, maxEnding);
35 }
36 return res;
37};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected