【Leetcode】Maximum Product Subarray
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的更多相关文章
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 【Leetcode】【Medium】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【数组】Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【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 ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
随机推荐
- Effective ObjectiveC 2.0 Note
[Effective ObjectiveC 2.0 Note] 1.The memory for objects is always allocated in heap space and never ...
- Spring总结七:AOP动态代理的实现
Spring中的AOP代理可以使JDK动态代理,也可以是CGLIB代理,前者基于接口,后者基于子类. 首先我们来用代码简单演示jdk动态代理: 现在有一个商品的增删改查的操作 /** * 商品操作接口 ...
- zookeeper集群安装的奇怪现象
zookeeper:配置的集群信息是domain:端口2888:端口3888: domain为内网静态ip:每次启动都不能相互连接报错误: [myid:3] - WARN [WorkerSende ...
- day63-webservice 03.解析cxf提供的例子
Path配置: C:\Program Files (x86)\ScanSign;E:\app\zhongzh\product\11.2.0\dbhome_1\bin;D:\app\zhongzh\pr ...
- Gym-101128D:Dice Cup
题意 给你两个骰子,一个有n面,一个有m面,分别仍一次,求和的概率最大的值 分析 签到题 模拟就行 凑数才把这个题也发到博客上···· #include <cstdio> #include ...
- IFM设备 Linux方面资料
Github: https://github.com/lovepark/ifm3d
- javac老提示无效的标记
加上-cp libs/*后,就开始提示无效的标记,搞了半天,似乎是shell展开的问题,估计是把后面的jar文件当源文件了? 加上引号就行了-cp "libs/*",不让shell ...
- sam格式详细说明
原文链接 https://www.jianshu.com/p/386f520e5de1 The SAM Format Specification(sam格式说明) 1 The SAM Format S ...
- RocketMq2
- django 1.8 官方文档翻译:13-12 验证器
django 1.8 官方文档翻译:13-12 验证器 2015年09月20日 21:36:18 ApacheCN_飞龙 阅读数:639 https://blog.csdn.net/wizardfo ...