【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 ...
随机推荐
- 虚拟机安装CentOS以及SecureCRT设置【完美无错版】
一.CentOS简介 CentOS是Linux的发行版之一,它安全.稳定.高效,是我最喜欢的Linux发行版之一.CentOS根据Red Hat Enterprise Linux开放源代码编译而成,与 ...
- Topic modeling【经典模型】
http://www.cs.princeton.edu/~blei/topicmodeling.html Topic models are a suite of algorithms that unc ...
- mybatis学习笔记 spring与mybatis整合
转载自http://blog.csdn.net/naruto_Mr/article/details/48239357 1.创建web工程,导入spring依赖包与mybatis依赖包,还需要mybat ...
- gitlab-ci配置疑难备忘
最近在自搭的gitlab服务器上加上了ci,大部份操作都比较顺利,但是也碰到一些问题抓狂,记录如下. 1.关于一个project配多个runner:在gitlab-ci里是支持的,但是含义确有点反常, ...
- 推荐一款基于XNA的开源游戏引擎《Engine Nine》
一.前沿导读 XNA是微软基于.Net部署的下一代3D/2D游戏开发框架,其实XNA严格来说类似下一代的DirectX,当然不是说XNA会取代DirectX,但是基于XNA我们对于面向XBOX360, ...
- Java 扫描器类 Scanner类
1.Scanner是SDK1.5新增的一个类,可是使用该类创建一个对象.Scanner reader=new Scanner(System.in); 2.reader对象调用下列方法(函数),读取用户 ...
- 异步串行通信的XON与XOFF
在单片机的异步串行通信中,putchar函数中的实现中反复用到了XON和XOFF,定义原型如下: #define XON 0x11#define XOFF 0x13 查找ASCII码表,这两个对应的是 ...
- CentOS下配置防火墙 配置nat转发服务
CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/sysconfig/network 这里可以更改主机 ...
- webrowser卡死解决方案
webrowser 是由于有道词典造成 解决方案,关闭有道或卸载:
- vs2015+opencv3.3.1 +Eigen 3.3.4 c++ 实现 泊松图像编辑(无缝融合)
#define EIGEN_USE_MKL_ALL #define EIGEN_VECTORIZE_SSE4_2 #include <iostream> #include "co ...