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 ( ...
随机推荐
- Chp18: Hard
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. Solut ...
- windows service 安装和卸载指令
添加服务: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319InstallUtil.exe D:\OneKeyWebSiteDeployment\Ser ...
- lintcode: 爬楼梯
题目: 爬楼梯 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 样例 比如n=3,中不同的方法 返回 3 解题: 动态规划题目,同时还是有顺序 ...
- PHP中的抽象类与接口
抽象类 php5支持抽象类和抽象方法.类前加 abstract, 此类就成为抽象类,无法被实例化,此类天生就是用来被继承的,给子类提供了一个类的模板; 类方法前加 abstract,是抽象方法,抽象方 ...
- SpringMVC学习总结(六)——SpringMVC文件上传例子(2)
基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下使用SpringMVC进行表单上的文件上传以及多个文件同时上传的不同方法 一.配置文件: SpringMVC 用的是 的 ...
- java @param参数注解
注解,@param是参数的解释.如/***@param s 这里表示对s的文字说明,描述 */ public void aa(String s){}一般java中@表示注解,解释一个方法,类,属性的作 ...
- NPOI技术,
using(FileStream stream=new FileStream("C:\Users\XXXXXX\Desktop\1.xls",FileMode.Open)) ...
- Shell最多支持多少个参数
本文转自:http://www.jb51.net/article/56548.htm 这篇文章主要介绍了Shell最多支持多少个参数?本文是对Shell最多可以输入多少个参数的一篇测试文章,需要的 ...
- 【问题】和NULL比较遇到的问题
1.问题描述: select FName from teacher where FId not in( select distinct FTeacherId from student ) 子查询返回的 ...
- JVM学习笔记(一)------基本结构
从Java平台的逻辑结构上来看,我们可以从下图来了解JVM: 从上图能清晰看到Java平台包含的各个逻辑模块,也能了解到JDK与JRE的区别 对于JVM自身的物理结构,我们可以从下图鸟瞰一下: 对于J ...