该题即是昨天没有做出来的题目,想了很久,想出了一个普通的做法,提交发现超时了。思想是新建一个数组,保存每个元素与后面的元素相乘后得到的最大值,然后再在该数组中选出最大的值,返回。【笨死

发现行不通后决定还是求教度娘了。

果然大神无处不在,该题可运用动态规划思想解决。考虑到正负数相乘后会出现的各种结果,采取保存局部最小和局部最大值的方式。列出公式:

int a=localmin*A[i]

int b=localmax*A[i]

localmin = min(A[i],min(a,b))

localmax = max(A[i],max(a,b))

ans = max(ans,localmax)返回ans 即为所求。

需再多思考运用该方法。

Maximum Product Subarray动态规划思想的更多相关文章

  1. 152. Maximum Product Subarray动态规划连乘最大子串

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

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

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

  3. LeetCode_Maximum Subarray | Maximum Product Subarray

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

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

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

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

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

  6. 152. Maximum Product Subarray - LeetCode

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

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

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

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

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

  9. LeetCode Maximum Product Subarray(枚举)

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

随机推荐

  1. Android之parseSDKContent failed

    由于之前安装ADT之后就一直报parseSDKContent failed的错误,具体的信息为:java.lang.NullPointerException. 此问题的情况为在Eclipse下,And ...

  2. 【获取图像处理源码以及编译过程】在window下make。

    google 找算法 发现一片不错论文,google作者的主页 找到了相关代码: http://cs.nyu.edu/~ccouprie/code.html code部分--------------- ...

  3. SR4000笔记

    长时间运行情况下需要有降温处理(40度以下) 建议使用触发模式而非连续模式 850nm波长的光(虽然无害,建议不要直视) 12V DC 滤波镜头(允许24个LED激光波长的通过) 快闪:获取数据 慢闪 ...

  4. Unity的Lerp函数实现缓动

    在Unity里面Lerp函数可以实现缓动效果 下面例子实现点光源的移动 在场景中创建好一个平面,一个点光源,我在这里随便放了一个模型. 然后新建c#脚本,代码如下: using UnityEngine ...

  5. 我的新顶级域名vell001.ml

    好不容易找到了一个免费的顶级域名啊,各种爽啊... 没钱的娃就只能各种求免费了!!! 以后访问我的所有站点都可以从vell001.ml开始了 Vell001主站 VellBlog VellForum

  6. Docker系列(五)OVS+Docker网络打通示例

    环境说明 两个虚拟机 操作系统Centos7 DOcker版本1.8 脚本内容: 1  4  7  10  19  27  32    33  39   -j ACCEPT 47    48  # R ...

  7. Codeforces10D–LCIS(区间DP)

    题目大意 给定两个序列,要求你求出最长公共上升子序列 题解 LIS和LCS的合体,YY好久没YY出方程,看了网友的题解,主要是参考aikilis的,直接搬过来好了 经典的动态规划优化. 用opt[i] ...

  8. HDU 5464 ( Clarke and problem ) (dp)

    dp[i][j] := 前i个数和为j的情况(mod p) dp[i][j] 分两种情况 1.不选取第i个数 -> dp[i][j] = dp[i-1][j] 2.   选取第i个数 -> ...

  9. HTML的id,name,class

    HTML中的id是给JavaScript用的(document.getElementById()) HTML中的name是给JavaScript用的(formUploadFile.submit()) ...

  10. 编程实例--for循环,找出0~100之间与8有关的正整数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...