lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列
题目
找出一个序列中乘积最大的连续子序列(至少包含一个数)。
比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6。
解题
法一:直接暴力求解
时间复杂度O(N2)
public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
*/
public int maxProduct(int[] nums) {
// write your code here
if(nums == null)
return 0;
int MAX = Integer.MIN_VALUE;
int n = nums.length;
for(int i=0;i<= n-1 ;i++){
int pro = 1;
for(int j = i;j< n;j++){
pro *= nums[j];
MAX = Math.max(MAX,pro);
}
}
return MAX;
}
}
Java Code
总耗时: 2412 ms
法二:利用动态规划
个人感觉不好写,这里的数组有整数也有负数,某子数组乘积最大,有两种情况:1,负数*负数,2,正数*正数,所以要考虑两种情况,我只用第二种求解时候,发现了问题,毕竟许多负数成绩时候也可能取得整数的。
负数当然要选取最小的负数了,正数要是最大的正数。
maxLocal = A[0]
minLocal = A[0]
global = A[0]
在变量A数组的过程中:
maxLocal = max(maxLocal*A[i],A[i],minLocal*A[i])
minLocal = min(maxLocal*A[i],A[i],minLocal*A[i])
global = max(maxLocal,global)
上面中间的A[i],是可能断了的情况,与之前求最大/小子数组的和是一个道理《乘以、加,减一个数,我们要选取我们需要的数咯》
public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
*/
public int maxProduct(int[] A) {
// write your code here
if(A == null || A.length ==0)
return 0;
int maxLocal = A[0];
int minLocal = A[0];
int global = A[0];
for(int i=1;i< A.length; i++){
int tmp = maxLocal;
maxLocal = Math.max(Math.max(A[i]*maxLocal,A[i]),
A[i]*minLocal);
minLocal = Math.min(Math.min(A[i]*tmp,A[i]),
A[i]*minLocal);
global = Math.max(maxLocal,global);
}
return global;
}
}
Java Code
总耗时: 1823 ms
class Solution:
# @param nums: an integer[]
# @return: an integer
def maxProduct(self, nums):
# write your code here
if nums == None or len(nums) ==0:
return 0
maxLocal = nums[0]
minLocal = nums[0]
Global = nums[0]
for i in range(1,len(nums)):
num = nums[i]
tmp = maxLocal
maxLocal = max(max(num*maxLocal,num),num*minLocal)
minLocal = min(min(num*tmp,num),num*minLocal)
Global = max(Global,maxLocal)
return Global
Python Code
总耗时: 376 ms
lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列的更多相关文章
- Maximum Product Subarray(最大连续乘积子序列)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Maximum Product Subarray 最大连续乘积子集
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【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 (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 46.Maximum Product Subarray(最大乘积子数组)
Level: Medium 题目描述: Given an integer array nums, find the contiguous subarray within an array (con ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
随机推荐
- Ping N个IP测试网络连通性
#-----------------------Smokeping移动节点-------------------##! /bin/bashecho "------------- Statin ...
- C#各种常用开源框架-支持开源!分享!
下面罗列了开发及学习过程中所涉及的开源类库的列表! AForge.NET Accord.NET NAudio nVLC Speex C# WebServer FFmpeg FFmpeg.NET Flo ...
- php header函数实例代码
一个完美的演示PHP header()函数用法的完整代码. 其中介绍的refresh方法,比<META ……用起来更得心应手,应该是段不错的代码. <?php /*** Function ...
- 浅析php fwrite写入txt文件的时候用 \r\n不能换行的问题
以下是对php中fwrite写入txt文件的时候用 \r\n不能换行的问题进行了介绍,需要的朋友可以过来参考下今天遇到了一个问题就是用fwrite写入txt文件的时候用 rn不能换行试了很久都没找到办 ...
- CSS圆角效果
看了院子里一篇关于CSS圆角技巧的文章,试了一下,觉得很好,贴出练习的代码.优秀文章链接: http://www.cnblogs.com/luluping/archive/2010/06/26/176 ...
- Oracle 10g RAC 启动与关闭
一. 检查共享设备 一般情况下,存放OCR和Voting Disk的OCFS2 或者raw 都是自动启动的. 如果他们没有启动,RAC 肯定是启动不了. 1.1 如果使用ocfs2的 检查ocfs2 ...
- 用Redis bitmap统计活跃用户、留存
Spool的开发者博客,描述了Spool利用Redis的bitmaps相关的操作,进行网站活跃用户统计工作. 原文:http://blog.getspool.com/2011/11/29/fast-e ...
- 学C++之感悟
程序设计真的就这么难得入门啊 最要命的事情就是看那些看不懂的书.断断续续地看C++Primer好几天了,还是一点眉目都没有,稀里糊涂的.看得头疼了用Google找过来人留下的东西看,无意中发现了一篇自 ...
- [ios]scrollView实现移动与缩放
实现滑动 1.在viewDidLoad中对scrollview的contentSize属性赋值 告诉他滑动范围. 实现缩放 1.在storyboard的scrollview的attribute标签中设 ...
- IOS开发之──应用之间调用(1)
iphone应用之间调用步骤: 1)在plist文件中,注册对外接口 在xcode group&files 里面,展开 resources选择<app>info.plist 鼠标右 ...