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 ...
随机推荐
- Gprinter Android SDK V2.0 使用说明
佳博特约经销商,此店购买的打印机问题优先解决哟 https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.V1p ...
- wpf 自定义RadioButton控件样式
实现的效果为: 我感觉来自定义RadioButton样式和定义button空间的样式差不多,只是类型不同而已. 接下来分析一下样式代码: <!--自定义单选按钮样式--> & ...
- (JAVA)从零开始之--打印流PrintStream记录日志文件
这里的记录日志是利用打印流来实现的. 文本信息中的内容为String类型.而像文件中写入数据,我们经常用到的还有文件输出流对象FileOutputStream. File file = new Fil ...
- extend简单用法
eg:var obj1=[{a:1,b:2},{a:2,b:3}] var obj2=[{c:3,d:2},{c:4,d:3}] var resultArray=[]; for (var i = 0; ...
- js 强制转换
强制转换为布尔类型: <script> var text =Boolean(0) //=>以下转换的类型都为false text = Boolean(0.0) text = Bool ...
- mysql数据类型——整型INT(m)
1.整形分为四种 tinyint smallint mediumint int bigint 注意: 右侧的取值范围是在未加unsigned关键字的情况下,如果加了unsigned,则最大值翻倍,如t ...
- nodebb在阿里云主机部署过程
1.在centos上安装nodejswget http://nodejs.org/dist/v0.8.9/node-v0.8.9.tar.gztar zxvf node-v0.8.9.tar.gzcd ...
- [转] Java 8的新特性
简介 毫无疑问,Java 8是Java自Java 5(发布于2004年)之后的最重要的版本.这个版本包含语言.编译器.库.工具和JVM等方面的十多个新特性.在本文中我们将学习这些新特性,并用实际的例子 ...
- laravel框架——线上环境错误总结
除了根目录,其他目录访问全是Not Found
- 通过GetManifestResourceStream加载文件出现错误提示“null值”对于“stream”无效[转]
本文解决了我的问题,收藏一下. 原文地址:http://blog.sina.com.cn/s/blog_a67799f601010atz.html 在做Mobile开发时,需要引入图片,用到了这个方法 ...