LintCode-乘积最大子序列
题目描述:
找出一个序列中乘积最大的连续子序列(至少包含一个数)。
比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6。
第一种解法,同最大和子序列的暴力求解法,直接求出每个子序列的乘积,取最大值。
public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
*/
public int maxProduct(int[] nums) {
int max = nums[0];
int index = 1;
while(index <= nums.length){
for(int i=0;i<=nums.length-index;i++){
int product = 1;
for(int j=0;j<index;j++){
product *= nums[i+j];
}
if(product > max)
max = product;
}
index ++;
}
return max;
}
}
同样,在数据量大的时候回超时,通过94%测试点。
第二种解法:
动态规划,每一步只需要记住其前一步的整数最大值和负数的最小值。代码如下:
public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
*/
public int maxProduct(int[] nums) {
int posmax=nums[0],negmax=nums[0],max=nums[0];
for(int i=1;i<nums.length;i++){
int tempPosMax = posmax;
int tempNegMax = negmax;
posmax = Math.max(nums[i],Math.max(nums[i]*tempPosMax,nums[i]*tempNegMax));
negmax = Math.min(nums[i],Math.min(nums[i]*tempPosMax,nums[i]*tempNegMax));
if(Math.max(posmax,negmax) > max){
max = Math.max(posmax,negmax);
}
}
return max;
}
}
LintCode-乘积最大子序列的更多相关文章
- Java实现 LeetCode 152 乘积最大子序列
152. 乘积最大子序列 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] ...
- [Swift]LeetCode152. 乘积最大子序列 | Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- leetcode 152. 乘积最大子序列 java
题目: 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. ...
- 152 Maximum Product Subarray 乘积最大子序列
找出一个序列中乘积最大的连续子序列(该序列至少包含一个数).例如, 给定序列 [2,3,-2,4],其中乘积最大的子序列为 [2,3] 其乘积为 6.详见:https://leetcode.com/p ...
- [算法]LeetCode 152:乘积最大子序列
题目描述: 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4]输出: 6解释: 子数组 [2,3] 有最大乘积 6.示 ...
- 【leetcode-152】 乘积最大子序列
给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4]输出: 6解释: 子数组 [2,3] 有最大乘积 6.示例 2: 输 ...
- LeetCode | 152. 乘积最大子序列
原题(Medium): 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 思路: 遍历数组时且逐元素相乘时,如果遇到了0,在求乘积最大值的情况下,0左边的元素 ...
- Leetcode题目152.乘积最大子序列(动态规划-中等)
题目描述: 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6 ...
- LeetCode 152. 乘积最大子序列(Maximum Product Subarray)
题目描述 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. ...
- [LeetCode]152. 乘积最大子序列(DP)
题目 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. 示 ...
随机推荐
- 基于注解的EF
首先得你的ef dll版本在4.1以上 第一步贴第一个类 由于字段太多就写一部分 [Table("NavF")]//设置表名称 public class NavF ...
- xcode UIImageView创建、图片加载、 音频文件播放、 延迟调用
代码创建 /** 创建UIImageView */ UIImageView * imageView=[[UIImageView alloc]init]; /** 设置尺寸位置 */ imageView ...
- jsp-forward跳转
在Web中可以使用<jsp:forward>指令,将一个用户的请求(request)从一个页面传递到另一个页面,即完成跳转的操作. 1.调整前页:tiaozhuan_a.jsp 代码: & ...
- find: missing argument to `-exec'
man find 发现 花括号要加 '' find ${LOG_BASE_DIR}$dir/ -type f -mtime +${KEEP_DAYS} -name ${LOG_REG} -exec r ...
- mongodb数据库调试问题:‘db object already connecting, open cannot be called multiple times’
在微博小系统的调试过程中: (1)登入登出可以正常显示,就是在注册的时候网络连接突然停止,但是用户名和密码已经存入数据库中,报错为:undefined is not a function 错误主要指向 ...
- jQuery给table中的负数标红色
<table class="tb_list"></table> $(function(){ $(".tb_list td").each( ...
- zoj 3792 Romantic Value
题目链接 求最小割的值, 以及割边最少的情况的边数. 先求一遍最小割, 然后把所有割边的权值变为1, 其他边变成inf, 在求一遍最小割, 此时求出的就是最少边数. Inf打成inf WA了好几发. ...
- Python正则表达式指南(转载)
转载自:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html#3353540 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不 ...
- Spring jdbctemplate学习笔记
/*List<?> config = getDB(" select t.datavalue from sys_config t where t.configid = '15' & ...
- Android 三大图片缓存原理、特性对比
这是我在 MDCC 上分享的内容(略微改动),也是源码解析第一期发布时介绍的源码解析后续会慢慢做的事. 从总体设计和原理上对几个图片缓存进行对比,没用到他们的朋友也可以了解他们在某些特性上的实现. 上 ...