Maximum Product Subarray——LeetCode
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.
题目大意:给定一个数组,找出最大连续子数组乘积。
解题思路:
解法一、我是用动态规划来解,因为都是整数,还是比较简单,从前往后遍历一次,需要维护比较三个变量,当前最小值,当前最大值,全局最大值。为什么需要当前最小值呢?不是求最大乘积嘛,因为如果当前数是负的,并且当前最小值也是负的,那么乘积可能就是下一个最大值。
LeetCode官方题解递推公式:
Let us denote that: f(k) = Largest product subarray, from index 0 up to k.
Similarly, g(k) = Smallest product subarray, from index 0 up to k.
Then, f(k) = max( f(k-1) * A[k], A[k], g(k-1) * A[k] )
g(k) = min( g(k-1) * A[k], A[k], f(k-1) * A[k] )
public int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int res = nums[0], min = nums[0], max = nums[0];
for (int i = 1; i < nums.length; i++) {
int curr_min = min * nums[i];
int curr_max = max * nums[i];
min = min(curr_min, curr_max, nums[i]);
max = max(curr_min, curr_max, nums[i]);
res = max(res, min, max);
}
return res;
}
int min(int a, int b, int c) {
int tmp = Math.min(a, b);
return Math.min(tmp, c);
}
int max(int a, int b, int c) {
int tmp = Math.max(a, b);
return Math.max(tmp, c);
}
解法二,上面说了,数组里都是整数。
①假设没有0,那么相乘的绝对值都是一直扩大的。所以可以这样考虑,偶数个负数,那么最大乘积就是所有的乘起来;要处理的就是奇数个负数的情况,这时就考虑舍弃左边第一个负数以左的乘积,或舍弃右边第一个负数以右的乘积。
②我们这里是有0的,我们可以考虑0将给定的数组划分为几个子数组,然后用①中的方式比较这些子数组中最大的乘积。
Talk is cheap>>
public int maxProduct2(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int max = Integer.MIN_VALUE;
int prod = 1;
for (int i = 0; i < nums.length; i++) {
prod *= nums[i];
max = Math.max(max, prod);
if (nums[i] == 0) {
prod = 1;
}
}
prod = 1;
for (int i = nums.length - 1; i >= 0; i--) {
prod *= nums[i];
max = Math.max(max, prod);
if (nums[i] == 0) {
prod = 1;
}
}
return max;
}
Maximum Product Subarray——LeetCode的更多相关文章
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- Maximum Product Subarray - LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- LeetCode_Maximum Subarray | Maximum Product Subarray
Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...
随机推荐
- Http,Https (SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2 中文帮助文档 分类: ASP.NET 2014-10-28 14:09 177人阅读 评论(1) 收藏
下载地址1:https://securityswitch.googlecode.com/files/SecuritySwitch%20v4.2.0.0%20-%20Binary.zip 下载地址2:h ...
- Bat命令知识[转]
基础部分: 一.基础语法: 1.批处理文件是一个".bat"结尾的文本文件,这个文件的每一行都是一条DOS命令.可以使用任何文本文件编辑工具创建和修改. 2.批处理是一种简单的程序 ...
- NetAdvantage webdatagrid 控件的一些属性
属性: 1 behaviors 行为下的属性集合 Row Selectors 主要用于设置行选择样式与形为的集合 Enable 属性表示是否启用 Row Selectors下的属性设置 RowNumB ...
- jQuery AJAX实现调用页面后台方法
1.新建demo.aspx页面.2.首先在该页面的后台文件demos.aspx.cs中添加引用. using System.Web.Services; 3.无参数的方法调用. 大家注意了,这个版本不能 ...
- StudioStyle 使用 厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试
厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试 http://studiostyl.es/ 去下载个自己喜欢的编码样式吧 如果你有想法 有能力 可以自己去做一个自己喜欢的 OK ...
- php100视频原始地址列表整理:
php100视频原始地址列表整理: 教程名称 . 1:环境配置与代码调试 2:PHP的数据类型与源码调试 3:常用PHP运算类型介绍与应用 4: PHP条件语句介绍与应用 5:PHP循环语句的介绍与应 ...
- Oracle分区表学习
(1) 表空间及分区表的概念表空间: 是一个或多个数据文件的集合,所有的数据对象都存放在指定的表空间中,但主要存放的是表, 所以称作表空间.分区表: 当表中的数据量不断增大,查询数据的速度就会变慢,应 ...
- C# div布局
本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. 转载请标明:http://www.kwstu.com/ArticleView/divcss_20139 ...
- 15_RHEL7挂载NTFS分区
1.下载ntfs-3g wget https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2015.3.14.tgz 2.安装 tar -zxvf ntfs-3 ...
- C# winform 递归选中TreeView子节点
/// <summary> /// 递归选中所有的自节点 /// </summary> /// <param name="nodeThis">T ...