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.

思路:类似Maximum Subarray,不同的是

  • max(max_local*nums[i], nums[i])表示:如果之前的数max_local和nums[i]符号相反,那么nums[i]会是max
  • max(max(max_local*nums[i], nums[i]), min_local*nums[i])表示:如果nums[i]是个负数,那么就可能和min_local组成局部最优=>这是为什么我们要设置min_local
class Solution {
public:
int maxProduct(vector<int>& nums) {
int max_local = nums[];
int min_local = nums[];
int max_copy;
int global = nums[]; for(int i = ; i < nums.size(); i++){
max_copy = max_local;
max_local = max(max(max_local*nums[i], nums[i]), min_local*nums[i]);
min_local = min(min(max_copy*nums[i], nums[i]), min_local*nums[i]);
global = max(global,max_local);
} return global;
}
};

152. Maximum Product Subarray (Array; DP)的更多相关文章

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

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

  2. 152. Maximum Product Subarray - LeetCode

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

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

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

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

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

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

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

  6. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

  7. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

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

  8. 152. Maximum Product Subarray(动态规划)

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

  9. 152. Maximum Product Subarray最大乘积子数组/是否连续

    [抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...

随机推荐

  1. IUSER 匿名帐户密码获取

    如何获取IUSR帐号和密码呢?有两种方法 1.安装IIS Resources,打开IIS Resources中的Metabase Explorer->机器名->LM->W3SVC-& ...

  2. ---github git clone 加速

    https://www.zhihu.com/question/27159393/answer/35528173 git config --global http.postBuffer 52428800 ...

  3. 配置maven访问nexus,配置项目pom.xml以发布maven项目到nexus中

    maven访问nexus有三种配置方法,分别为: 项目pom.xml,优先级最高: user的settings.xml,优先级中,未在pom.xml中配置repository标签,则使用这个配置: m ...

  4. js弹出div层内容(按回退键关闭div层及遮罩)

    <!--弹出的div列表对应的详情--> <div id="newhtml" class="white_content"> <di ...

  5. python语言中的数据类型之字典

    数据类型 字典类型dict 用途:记录多个值,列表是索引对应值,而字典是key对应值,其中key对value有描述性 定义方式:在{ }用逗号分隔开多个元素,每个元素都是key:value形式,其中k ...

  6. Servlet开发

    源地址:http://www.cnblogs.com/xdp-gacl/p/3760336.html 一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun ...

  7. LG Optimus L90 [D415] T-Mobile 刷机

    1 先使用[ROOT大师]ROOT手机. 2 执行以下ADB命令. adb shell su //备份 dd /by-name/laf of=/sdcard/laf.img. //清除 dd /by- ...

  8. 使用navigator对象,输出当前浏览器的信息

    <script type="text/javascript"> with(document) { write("你的浏览器信息:<ol>" ...

  9. C#调用Delphi接口(ITest = interface)

    首先创建一个delphi的DLL工程 library testintfdll; { Important note about DLL memory management: ShareMem must ...

  10. easyUi 表头排序按钮 bug

    参考文章:https://www.shiqidu.com/p/81