Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example
For example, given the array [2,3,-2,4]
, the contiguous subarray[2,3]
has the largest product = 6
.
分析:
因为这题要求乘积最大,而负数和负数相乘可是是正数,所以,我们必须得保存两个变量,即当到达A[i]时,前一个数A[i - 1]所对应的最大值和最小值max[i - 1] 和 min[i - 1]. max[i] 和min[i]和这些值有如下关系:
min[i] = min(nums[i], min[i - 1] * nums[i], max[i - 1] * nums[i]);
max[i] = max(nums[i], min[i - 1] * nums[i], max[i - 1] * nums[i]);
public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
* cnblogs.com/beiyeqingteng/
*/
public int maxProduct(int[] nums) {
if (nums == null || nums.length == ) return ;
if (nums.length == ) return nums[]; int[] min = new int[nums.length];
int[] max = new int[nums.length]; min[] = nums[];
max[] = nums[]; for (int i = ; i < nums.length; i++) {
min[i] = min(nums[i], min[i - ] * nums[i], max[i - ] * nums[i]);
max[i] = max(nums[i], min[i - ] * nums[i], max[i - ] * nums[i]);
} int tempMax = max[];
for (int i = ; i < max.length; i++) {
if (tempMax < max[i]) {
tempMax = max[i];
}
}
return tempMax; } public int max(int a, int b, int c) {
return Math.max(a, Math.max(b, c));
} public int min(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
}
更简洁的做法:
public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
* cnblogs.com/beiyeqingteng/
*/
public int maxProduct(int[] nums) {
if (nums == null || nums.length == ) return ;
if (nums.length == ) return nums[]; int pre_temp_min = nums[];
int pre_temp_max = nums[];
int global_max = nums[]; for (int i = ; i < nums.length; i++) {
int temp_min = min(nums[i], pre_temp_min * nums[i], pre_temp_max * nums[i]);
int temp_max = max(nums[i], pre_temp_min * nums[i], pre_temp_max * nums[i]);
pre_temp_min = temp_min;
pre_temp_max = temp_max;
global_max = Math.max(global_max, temp_max);
}
return global_max;
} public int max(int a, int b, int c) {
return Math.max(a, Math.max(b, c));
} public int min(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
}
转载请注明出处:cnblogs.com/beiyeqingteng/
Maximum Product Subarray的更多相关文章
- 求连续最大子序列积 - 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(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- LeetCode_Maximum Subarray | Maximum Product Subarray
Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...
- 【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 ( ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- [LeetCode] 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 ...
随机推荐
- SSH框架总结(框架分析+环境搭建+实例源码下载)
来源于: http://blog.csdn.net/shan9liang/article/details/8803989 首先,SSH不是一个框架,而是多个框架(struts+spring+hiber ...
- 统计"1"个数问题
问题: 给定一个十进制整数N,求出从1到N的所有整数中出现”1”的个数. 例如:N=2时 1,2出现了1个 “1” . N=12时 1,2,3,4,5,6,7,8,9,10,11,12.出现了5个“1 ...
- BZOJ-1491 社交网络 FLoyd+乱搞
感觉这两天一直在做乱搞的题... 1491: [NOI2007]社交网络 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1279 Solved: 732 ...
- codeforces 719A:Vitya in the Countryside
Description Every summer Vitya comes to visit his grandmother in the countryside. This summer, he go ...
- ubuntu安装spark
1.先得准备环境,需要JAVA环境,还有Python环境(默认会有) JAVA下载JDK之后配置JAVA环境变量 export JAVA_HOME=/opt/jdk1..0_45 export JRE ...
- 数数字 (Digit Counting,ACM/ICPC Danang 2007,UVa 1225)
思路: 利用java 特性,将数字从1 一直加到n,全部放到String中,然后依次对strring扫描每一位,使其carr[str.charAt(i)-'0']++; 最后输出carr[i],即可. ...
- hdu 1048 The Hardest Problem Ever
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(Strin ...
- 锋利的jQuery-3--用js给多选的checkbox或者select赋值
单选的select: <select id="single"> <option>Single</option> <option>Si ...
- ccleaner注册码
名称:Registered User 密钥:CBB4-FJN4-EPC6-G5P6-QT4C 先不要启动程序,然后断网注册.
- Source Insight 基本使用(1)-使用Source Insight查看Android Framework 源码
一.下载framework源码: google已经把framework源码托管在了gitHub上: https://github.com/android/platform_frameworks_bas ...