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.

求连续子序列最大乘积,类似于求连续子序列最大和,但是乘法还涉及到符号的问题,比较方便的方法是同时维护到当前元素的最大乘积和最小乘积。

 class Solution {
public:
int maxProduct(int A[], int n) { int max_ending_here = A[];
int min_ending_here = A[]; int max_so_far = A[]; for (int i = ; i < n; ++i) {
int p_max = A[i] * max_ending_here;
int p_min = A[i] * min_ending_here; max_ending_here = max(A[i], max(p_max, p_min));
min_ending_here = min(A[i], min(p_max, p_min)); max_so_far = max(max_so_far, max_ending_here);
} return max_so_far;
}
};

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

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

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

  2. 【Leetcode】【Medium】Maximum Product Subarray

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

  3. 【数组】Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  4. 【leetcode】Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

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

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

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

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

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

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

  8. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  9. 【leetcode】Maximum Subarray

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

随机推荐

  1. ORA-00372此时无法修改文件5 ORA-01110数据文件5'M:\WWFDATA.dbf'

    错误提示如下图: ORA-00372此时无法修改文件5 ORA-01110数据文件5'M:\DB_DATA\SEINESCMDB\WWFDATA_DATA01.dbf' 分析原因及解决方法: 1.查看 ...

  2. 六.使用list和tuple

    list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: classmates = ...

  3. win10手动开启wifi

    win+R键,输入cmd,以管理员身份运行,输入netsh wlan set hostednetwork mode=allow ssid=wifi key=wifimima123回车 解释一下: ss ...

  4. laravel中的attach and detach toggle method

    创建模型 post  and  user 以及 users , posts ,user_post(favorities)测试数据 在此可以看上一篇中的数据,本次测试数据利用的上一篇的数据.detach ...

  5. IE8下javascript的时间函数Date()不兼容问题,显示NAN【转】

    function parseISO8601(dateStringInRange) {   var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,       dat ...

  6. Java集合框架---重构设计

    面向接口编程: 接口类型 变量 =new 实现类(); List  list=new ArrayList(); --------------------------------------- List ...

  7. Redesign Your App for iOS 7 之 页面布局【转】

    前言 iOS7是目前iOS史上最颠覆的一次改版. 它的出现令人兴奋,因为它将会带我们进入一个全新的时代: 它的到来也让我们忧心,因为它颠覆了一切,包括我们过去做过的很多努力. 但是,相信大家乐意为这个 ...

  8. (转)菜鸟去重复之Sql

    原文地址:http://www.cnblogs.com/fatbird/p/Sql-Remove-duplicate.html 前言 本文主要是总结平时工作学习中遇到的使用Sql Server的去除重 ...

  9. 三维GIS

    三维GIS数据结构 三维GIS数据库 三维渲染显示 点云处理 cnki:http://kns.cnki.net/kns/brief/default_result.aspx

  10. 编写高质量代码改善C#程序的157个建议——建议54:为无用字段标注不可序列化

    建议54:为无用字段标注不可序列化 序列化是指这样一种技术:把对象转变成流.相反过程,我们称为反序列化.在很多场合都需要用到这项技术. 把对象保存到本地,在下次运行程序的时候,恢复这个对象. 把对象传 ...