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 ( ...
随机推荐
- HDU 1829 A Bug's Life(种类并查集)
思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...
- C#学习笔记:泛型委托Action<T>和Fun<TResult>
转自:http://www.cnblogs.com/Joetao/articles/2094271.html 本节学习了泛型委托Action<T>和Fun<TResult>两类 ...
- the-type-java-lang-charsequence-cannot-be-resolved-in-package-declaration
http://stackoverflow.com/questions/24301986/the-type-java-lang-charsequence-cannot-be-resolved-in-pa ...
- codeforces div.1 A
A. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Debug过程中的mock (及display窗口的使用)
转载:http://m.blog.csdn.net/blog/u012516903/18004965 在debug的时候,有3个地方可以进行mock测试 测试代码如下: 1.使用display窗口 W ...
- iOS LLDB调试器
随着Xcode 5的发布,LLDB调试器已经取代了GDB,成为了Xcode工程中默认的调试器.它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.LLDB为Xcode提供了底层调试环 ...
- Excel多条件筛选求和
单位A 代码B 面积(㎡)C A组 011 124 A组 123 15 A组 011 356 A组 123 44 B组 123 31 B组 011 2 B组 123 2 按照单位和代码求面积的和,可以 ...
- Android 拦截短信
public class SMSMess extends BroadcastReceiver { @Override public void onReceive(Context arg0, Inten ...
- apache&nginx资料汇总
http://liudaoru.iteye.com/blog/336338 aquid:http://os.51cto.com/art/201009/225813.htm 数据库各种讲座:http:/ ...
- [POJ3352]Road Construction(缩点,割边,桥,环)
题目链接:http://poj.org/problem?id=3352 给一个图,问加多少条边可以干掉所有的桥. 先找环,然后缩点.标记对应环的度,接着找桥.写几个例子就能知道要添加的边数是桥的个数/ ...