LeetCode之Maximum Product Subarray
1.(原文)问题描述
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.
2.翻译
找最大的数组中的子数组乘积 例如,给予的数组是[2,3,-2,4]
连续的字数组[2,3]有最大的乘积=6
3.解决思路分析
肯定可以通过其他方式实现就是不断的循环遍历,可是这样的代价太大,时间复杂度很高,我们j可以转换下思路好好想想如何最高效的来解决这个问题。
既然是求最大乘积那么我们需要考虑的就是最大乘积出现的情况可能有哪些:很显然有两种,第一种就是最大的累积乘以一个正数那么我们获取了更大的值,
还有就是一个最小的累积的值乘以一个负数,那么也可能获取的是最大的值,因为我们只需要保存乘积的最大和最小值。我们可以把数组的第一个值作为
最大最小值的逻辑值来处理,然后进行下去;如果之前的最大和最小值同当前元素相乘之后,没有当前元素大(或小)那么当前元素就可作为新的起点。
4.实现过程
Solution.java
package MaximumSubArray;
public class Solution {
public int maxProduct(int[] A) {
if(A == null||A.length==0){
return 0;
}
int maxProduct = A[0]; //最大值的初始值
int maxTemp = A[0]; //累积的最大值
int minTemp = A[0]; //累积的最小值
for(int i = 1;i < A.length;i++) {//从数组的第二个元素开始遍历
int a = A[i]*maxTemp;
int b = A[i]*minTemp;
maxTemp = Math.max(Math.max(a,b), A[i]);//选取最大值的新起点
minTemp = Math.min(Math.min(a,b), A[i]);//选取最小值的新起点
maxProduct = Math.max(maxProduct, maxTemp);//更新最大值
}
return maxProduct;
}
}
SolutionTest.java
package MaximumSubArray;
public class SolutionTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int []array1 = {0,2,3,5,6,8,0,9};
int []array2 = {1,-2,-3,-5,6,8,0,9};
int []array3 = {1,2,-3,5,6,8,0,9};
int []array4 = {1,2,0,5,6,8,0,9};
System.out.println(new Solution().maxProduct(array1));
System.out.println(new Solution().maxProduct(array2));
System.out.println(new Solution().maxProduct(array3));
System.out.println(new Solution().maxProduct(array4));
}
}
5.有句话很正确,解决思路很重要,重要的是思想。
LeetCode之Maximum Product Subarray的更多相关文章
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetCode][001] Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- Java for LeetCode 152 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 152. Maximum Product Subarray --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- C#解leetcode 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]152. Maximum Product Subarray最大乘积子数组
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
随机推荐
- asp.net mvc实现rest风格返回json
实现类似:http://localhost:1799/rest/person/1方式返回一个json内容: 在asp.net mvc中新建一个control rest,然后在其中新增方法: publi ...
- Android 在非主线程无法操作UI意识
Android在应用显示Dialog是一个非常easy事儿,但我从来没有尝试过Service里面展示Dialog. 经验UI操作要在主线程,本地的服务Service是主线程里没错,可是远程servic ...
- Oracle拉进sqlserver表声明的建设
我们将Oracle数据被同步到sqlserver时间,早餐在sqlserver表中的端构造. 我们是不同步的复杂领域,只考虑以下四种数据类型. Oracle到SQLServer做的映射: int -& ...
- [转]从.NET转JAVA开发
[转]从.NET转JAVA开发 .NET转JAVA其实也很简单,自己动手写几个DEMO差不多就了解了 1. JAVA做互联网开发多半只会用到开源框架Struts2 Hibernate 和Spring, ...
- 具体解释首页被K后SEOer必做的三大排除方法!
近段时间.有非常多朋友向新辰抱怨说出大问题了,为神马site不到首页了,并且收录变成了0?唉,新辰不得不非常同情的告诉你:你的首页真的被K了!好了.作为一个职业SEOer.面对被K宛如已经看破红尘般没 ...
- 获得mysql内容,生成xml文件,另外,为了webservice发送
<xyt> <news id> <url></url> <title></title> </news> </x ...
- SICP 习题 (1.37)解题总结
SICP 习题 1.37是一条非常长的题目,主要讲的是无穷连分式.无穷连分式对我来说又是一个陌生的概念,于是又去百度了一番,发现无穷连分式也是一个非常有意思的话题,涉及到无理数的表达.只是我建议大家还 ...
- leetcode 刷道题 70 earch Insert Position 二进制搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- asp.net学习之 数据绑定控件--表格绑定控件
原文:asp.net学习之 数据绑定控件--表格绑定控件 数据绑定 Web 服务器控件是指可绑定到数据源控件,以实现在 Web 应用程序中轻松显示和修改数据的控件.数据绑定 Web 服务器控件 ...
- Lake Counting (DFS)
N*M的园子,雨后积起了水.八连通的积水背认为是连接在一起的.请求出园子里总共有多少水洼? dfs(Depth-First Search) 八个方向的简单搜索.... 深度优先搜索从最开始的状态出 ...