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 1905 小数化分数2
;}
- Cache 应用程序数据缓存
System.Web.Caching 命名空间提供用于缓存服务器上常用数据的类.此命名空间包括 Cache 类,该类是一个字典,您可以在其中存储任意数据对象,如哈希表和数据集.它还为这些对象提供了失效 ...
- http://www.aboutyun.com/thread-8792-1-1.html
http://www.aboutyun.com/thread-8792-1-1.html
- lintcode:Flip Bits 将整数A转换为B
题目: 将整数A转换为B 如果要将整数A转换为B,需要改变多少个bit位? 样例 如把31转换为14,需要改变2个bit位. ()10=()2 ()10=()2 挑战 你能想出几种方法? 解题: A- ...
- java @param参数注解
注解,@param是参数的解释.如/***@param s 这里表示对s的文字说明,描述 */ public void aa(String s){}一般java中@表示注解,解释一个方法,类,属性的作 ...
- Tomcat就是个容器,一种软件
1.tomcat就是一个容器而已,一个软件,运行在java虚拟机. 2.tomcat是一种能接收http协议的软件,java程序猿自己也可以写出http解析的服务器啊. 3.tomcat支持servl ...
- HTML5入门2---js获取HTML元素的值
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Java学习笔记之:Java的变量
一.介绍 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identifier [= value] ...] ; 格式 ...
- GridView 和ListView中自适应高度
android中GridView 和ListView放在scrollView中时会默认的只有一行高这时就要我们自己计算出它的高度啦 首先是listview的 //动态设置listview的高度 pu ...
- 想要风投被你的融资 PPT 打动吗?别忘了你其实就是在想方设法卖出自己公司的部分股权
硅谷,一个常常见诸于报端,看着很熟悉,但是又不那么被人所了解的未及之地.它不是一个严格限定的地理位置,一般来说是指旧金山和湾区,其中湾区又分为东湾(East Bay)和南湾(South Bay), ...