/* * @lc app=leetcode id=152 lang=javascript * * [152] Maximum Product Subarray * * https://leetcode.com/problems/maximum-product-subarray/description/ * * algorithms * Medium (28.61%) * Total Accepted: 202.8K * Total Submissions: 700K * Testcase Example: '[2,3,-2,4]' * * Given an integer array nums, find the contiguous subarray within an array * (containing at least one number) which has the largest product. * * Example 1: * * * Input: [2,3,-2,4] * Output: 6 * Explanation: [2,3] has the largest product 6. * * * Example 2: * * * Input: [-2,0,-1] * Output: 0 * Explanation: The result cannot be 2, because [-2,-1] is not a subarray. * */ /** * @param {number[]} nums * @return {number} */ var maxProduct = function(nums) { // let max = nums[0]; // let temp = null; // for(let i = 0; i < nums.length; i++) { // temp = nums[i]; // max = Math.max(temp, max); // for(let j = i + 1; j < nums.length; j++) { // temp *= nums[j]; // max = Math.max(temp, max); // } // } // return max; let max = nums[0]; let min = nums[0]; let res = nums[0]; for (let i = 1; i < nums.length; i++) { let tmp = min; min = Math.min(nums[i], Math.min(max * nums[i], min * nums[i])); // 取最小 max = Math.max(nums[i], Math.max(max * nums[i], tmp * nums[i])); /// 取最大 res = Math.max(res, max); } return res; };