Maximum Product Subarray JAVA实现
题目描述:
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.
分析:
这里主要得考虑两个因素:1、头元素到第一个0元素之间和两个0元素之间负数的个数;2、当遇到0时,应该重新计算当前的最大值
设计的思想:
1、本题为了更加方便,我使用了额外的空间,且空间复杂度为O(n)
2、第一个数组主要用来计算从头或者从0开始相乘的值,第二个数组是用来计算从头或者0后面第一个负数开始每个元素乘积。
例:(由于表达不好,上面可能解释的不是太清楚)
原始序列:
2 |
4 |
-1 |
3 |
4 |
0 |
2 |
-1 |
3 |
-1 |
oneValue:
2 |
4 |
-4 |
-12 |
-48 |
0 |
2 |
-2 |
-6 |
6 |
twoValue:
2 |
4 |
-4 |
3 |
12 |
0 |
2 |
-2 |
3 |
-3 |
代码:
package com.edu.leetcode; import javax.xml.transform.Templates; public class MaximumProductSubarray { public int maxProducts(int[] A) {
if(A.length==1){ //如果数组中只有一个元素直接输出
return A[0];
}
int[] oneValues = new int[A.length]; //辅助空间1,记录从头或者从0开始到当前位置的乘积
int[] twoValues=new int[A.length]; //辅助空间2,从头或者从0开始遇到第一负数时,后面的值重新从当前位置开始
oneValues[0]=A[0];
twoValues[0]=A[0];
boolean first =true; //从头或者从0开始,判断是否遇到了负数
for(int i=1;i<A.length;i++){
if(oneValues[i-1]==0){
oneValues[i]=A[i];
twoValues[i]=A[i];
first=true;
}
else{
if(A[i-1]<=-1&&first){ //当前面的一个数是从头或者从0开始的第一个负数时,将twoValues[i]的值赋值为A[i]
twoValues[i]=A[i];
oneValues[i]=oneValues[i-1]*A[i];
first=false;
}
else{
oneValues[i]=oneValues[i-1]*A[i];
twoValues[i]=twoValues[i-1]*A[i];
}
}
}
int maxValue=oneValues[0];
for(int i=0;i<oneValues.length;i++){
if(oneValues[i]>maxValue)
maxValue=oneValues[i];
if(twoValues[i]>maxValue)
maxValue=twoValues[i];
}
return maxValue;
} public static void main(String[] args) {
// TODO Auto-generated method stub
MaximumProductSubarray mps = new MaximumProductSubarray();
int[] s = {2,3,-1};
System.out.println(mps.maxProducts(s)); } }
Maximum Product Subarray JAVA实现的更多相关文章
- leetcode 152. Maximum Product Subarray --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- 求连续最大子序列积 - 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 ( ...
随机推荐
- mac上eclipse上运行word count
1.打开eclipse之后,建立wordcount项目 package wordcount; import java.io.IOException; import java.util.StringTo ...
- node.js 安装、图文详解
网上的教程很多,有些模糊不清,有些版本太旧,有些是.exe安装,本文讲述windows系统下简单nodejs .msi环境配置.最新版是Current version: v0.10.26.官网下载地址 ...
- 此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭“的解决
此版本的 SQL Server 不支持用户实例登录标志.该连接将关闭“的解决(转) 2008-10-04 13:31 错误提示:说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息 ...
- lintcode:Unique Characters 判断字符串是否没有重复字符
题目: 判断字符串是否没有重复字符 实现一个算法确定字符串中的字符是否均唯一出现 样例 给出"abc",返回 true 给出"aab",返回 false 挑战 ...
- 应用程序加载外部字体文件(使用AddFontResource API函数指定字体)
/* MSDN: Any application that adds or removes fonts from the system font table should notify other w ...
- 52. N-Queens II
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- UML与数据流图
Ref: <数据库设计理论及应用(3)——需求分析及数据>http://wenku.baidu.com/link?url=hbhJFytMKT8A ...
- devDependencies和dependencies的区别
我们在使用npm install 安装模块或插件的时候,有两种命令把他们写入到 package.json 文件里面去,比如: --save-dev --save 在 package.json 文件里面 ...
- poj 2891 Strange Way to Express Integers (扩展gcd)
题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2 ...
- Jqgrid入门-结合Struts2+json实现数据展示(五)
DEMO用的是ssh框架实现的,具体怎么搭建的就不多做说明了.分页表格的数据操作难点就是数据展现.至于增删改直接用hibernate原生的方法实现即可. 初步分析:表格要实现分页,那么 ...