【LeetCode】152. 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(vector<int>& nums) {
if (nums.empty()) {
return ;
}
int res = nums[];
for (int i = , imax = res, imin = res; i < nums.size(); ++i) {
if (nums[i] < ) {
swap(imax, imin);
}
// 幸运的是,这样的做法对数值0也同样有效
imax = max(nums[i], imax * nums[i]);
imin = min(nums[i], imin * nums[i]);
res = max(res, imax);
}
return res;
}
};
【LeetCode】152. Maximum Product Subarray的更多相关文章
- 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- LeetCode OJ 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...
- 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- 使用WebGL加载Google街景图
我们要实现的功能比较简单:首先通过坐标定位.我的位置.地址搜索等方式,调用google map api获取地址信息.然后根据地址信息中的全景信息获取当前缩放级别的全景信息.最终把这些全景信息通过Web ...
- Centos7.0下将Python更新到Python2.7.13
在云服务器下默认安装的python版本过低,所有我们要手动进行更新(不建议卸载老的版本,然后安装新的,这样会导致大量的异常错误) 为了防止在安装编译python时出错,需先更新gcc :yum - ...
- angularjs里重要的route
写一段代码来解释吧! <!DOCTYPE html><html ng-app="mainApp"><head lang="en"& ...
- [原创]CentOS实现智能DNS
一. 环境: Centos-6.6-x64位操作系统,IP地址:210.38.248.7 二. 安装和配置bind服务: 1. 命令:yum install bind ...
- Zepto源码分析-zepto模块
源码 // Zepto.js // (c) 2010-2015 Thomas Fuchs // Zepto.js may be freely distributed under the MIT lic ...
- python unittest 测试笔记(二):使用Requests
1. Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用.[Python Requests快速入门 :]http://cn.python-requests.org/z ...
- 生成简单的php验证码
之前发表过,但是上面只是一个截图,不便于大家复制和使用,所以在这重新发表一遍. <?php //生成验证码图片 Header("Content-type: image/JPEG&quo ...
- python基础操作
1.打印操作 print('2222') 2.接收用户输入 name=input('name') 3.if else判断 name='qiao'name2='师弟'username=input('输入 ...
- Codility---EquiLeader
Task description A non-empty zero-indexed array A consisting of N integers is given. The leader of t ...
- Ajax请求,跨域小坑
今天在上班的时候,被坐在旁边项目经理叫过去问了一个Ajax请求跨域的问题,一开始没理解清楚也还有对这个没有理解的透,后面被打击的要死. 当时的需求是需要测试一个已发布的api接口,需要在本地写测试程序 ...