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 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
随机推荐
- Andriod docs加载速度慢的问题解决
网上找了个类, import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import ja ...
- C#使用SocketAsyncEventArgs操作套接字的简单异步通讯
SocketAsyncEventArgs是一个套接字操作的类,主要作用是实现socket消息的异步接收和发送,跟Socket的BeginSend和 BeginReceive方法异步处理没有多大区别,它 ...
- 51nod1270 数组的最大代价(简单dp)
---恢复内容开始--- 1270 数组的最大代价 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 数组A包含N个 ...
- php对数组排序代码
php对数组排序,介绍了和php,有关的知识.技巧.经验,和一些php源码等. 对数组排序 usort() 函数使用用户自定义的函数对数组排序. */ function cmp($a, $b) //用 ...
- 11g RAC R2 体系结构---进程,日志
进程结构:Overview of Oracle Clusterware Platform-Specific Software Components When Oracle Clusterware is ...
- 1049. Counting Ones/整数中1出现的次数(从1到n整数中1出现的次数)
The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...
- db2查看表空间
select substr(tbsp_name,1,20) as 表空间名称,substr(tbsp_content_type,1,10) as 表空间类型,sum(tbsp_total_size_k ...
- Ubuntu下配置samba服务器实现文件共享
安装Samba 安装samba sudo apt-get install samba Kubuntu 安装系统设置的共享模块 sudo apt-get install kdenetwork-files ...
- TTL值的含义以及与域名DNS TTL值的区别
TTL值的含义以及与域名TTL值的区别 本文来源于时光漂流瓶 http://www.9usb.net , 原文地址: http://www.9usb.net/201004/ttl-yuyuming-t ...
- PHP前端$.ajax传递数据到后台
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...