题目:

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.

思路:

其实子数组乘积最大值的可能性为:累乘的最大值碰到了一个正数;或者,累乘的最小值(负数),碰到了一个负数。所以每次要保存累乘的最大(正数)和最小值(负数)。同时还有一个选择起点的逻辑,如果之前的最大和最小值同当前元素相乘之后,没有当前元素大(或小)那么当前元素就可作为新的起点。例如,前一个元素为0的情况,{1,0,9,2},到9的时候9应该作为一个最大值,也就是新的起点,{1,0,-9,-2}也是同样道理,-9比当前最小值还小,所以更新为当前最小值。

/**
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function(nums) {
var pro=nums[0],maxpro=nums[0],minpro=nums[0];
for(var i=1;i<nums.length;i++){
var maxtemp=maxpro;
maxpro=Math.max(Math.max(maxpro*nums[i],minpro*nums[i]),nums[i]);
minpro=Math.min(Math.min(maxtemp*nums[i],minpro*nums[i]),nums[i]);
pro=Math.max(maxpro,pro);
} return pro;
};

【数组】Maximum Product Subarray的更多相关文章

  1. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  2. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  3. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

  4. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  5. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  6. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  7. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  8. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  9. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

随机推荐

  1. ArcGIS Desktop Python add-ins 插件类型

    Python可以在ArcGIS 桌面程序中创建如下的插件类型: 插件类型 描述 按钮(button) 是一个简单功能定制形式,可以在点击情况下执行一些业务逻辑.它会执行一个或一系列的动作.按钮可以放在 ...

  2. Sensor fusion(传感器融合)

    From Wikipedia, the free encyclopedia 来自维基百科,免费的百科Sensor fusion is combining of sensory data or data ...

  3. Ubuntu 14.04 LTC 有线网络--网线不识别,灯不亮问题

    sudo ethtool -s eth0 autoneg off speed 100 duplex full

  4. HDU1181 变形课(DFS) 2016-07-24 13:31 73人阅读 评论(0) 收藏

    变形课 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒 ...

  5. NC入门笔记

    简介: NC(全名NetCat),在网络界有"瑞士军刀"的美誉.它小巧而强悍,被设计成一个稳定的后门工具,能够直接由其它程序和脚本轻松驱动.同时,它也是一个功能强大的网络调试和探测 ...

  6. POJ1269求两个直线的关系平行,重合,相交

    依旧是叉积的应用 判定重合:也就是判断给定的点是否共线的问题——叉积为0 if(!cross(p1,p2,p3) && !cross(p1,p2,p4))printf("LI ...

  7. The Real Meaning of Peace

    The Real Meaning of Peace There once was a king who offered a prize to the artist who would paint th ...

  8. Paul Simon -- Duncan

    Paul Simon -- Duncan (London,January 1972) Couple in the next roomBound to win a prizeTheyve been go ...

  9. vc6.0 PK vs2010

    从VC++6.0不足看VisualC++2010新特性 说起VC,有人想到维生素C(维C),有人想到风险投资(venture capital), 程序员们尤其是做底层开发的程序员或老程序员们第一感觉肯 ...

  10. LeetCode143:Reorder List

    题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must d ...