[nowCoder] 子数组最大乘积
给定一个double类型的数组arr,其中的元素可正可负可0,返回子数组累乘的最大乘积。例如arr=[-2.5,4,0,3,0.5,8,-1],子数组[3,0.5,8]累乘可以获得最大的乘积12,所以返回12。
分析,是一个dp的题目,
设f[i]表示以i为结尾的最大值,g[i]表示以i结尾的最小值,那么
f[i+1] = max{f[i]*arr[i+1], g[i]*arr[i+1],arr[i+1]} ,只有这三种情况。
考虑到f[i],g[i]只和i-1有关,那么可以用局部变量即可搞定,而不用使用数组。
http://www.nowcoder.com/profile/864393/test/231563/24590
class Solution {
public:
double maxProduct(vector<double> arr) {
if(arr.size() == )
return ;
double minVal = arr[];
double maxVal = arr[];
double rtn = arr[];
double tmpMax = ;
double tmpMin = ;
for(int i = ; i < arr.size(); i++)
{
//cout << "max\t" << maxVal << endl;
//cout << "min\t" << minVal << endl;
tmpMax = max(maxVal * arr[i], minVal * arr[i]);
tmpMin = min(maxVal * arr[i], minVal * arr[i]);
maxVal = max(tmpMax, arr[i]);
minVal = min(tmpMin, arr[i]);
rtn = max(rtn, maxVal);
}
return rtn;
}
};
[nowCoder] 子数组最大乘积的更多相关文章
- LeetCode OJ:Maximum Product Subarray(子数组最大乘积)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 152.Maximum Product Subarray---dp---连续子数组的最大乘积---《编程之美》2.13子数组的最大乘积
题目链接:https://leetcode.com/problems/maximum-product-subarray/description/ 题目大意:给出一串数组,找出连续子数组中乘积最大的子数 ...
- 连续子数组的最大乘积及连续子数组的最大和(Java)
1. 子数组的最大和 输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值.例如数组:arr[]={1, 2, 3, -2, ...
- 给定一个double类型的数组arr,其中的元素可正可负可0,返回子数组累乘的最大乘积。例如arr=[-2.5,4,0,3,0.5,8,-1],子数组[3,0.5,8]累乘可以获得最大的乘积12,所以返回12。
分析,是一个dp的题目, 设f[i]表示以i为结尾的最大值,g[i]表示以i结尾的最小值,那么 f[i+1] = max{f[i]*arr[i+1], g[i]*arr[i+1],arr[i+1]} ...
- [LeetCode] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- [Swift]LeetCode713. 乘积小于K的子数组 | Subarray Product Less Than K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- Java实现 LeetCode 713 乘积小于K的子数组(子集数量+双指针)
713. 乘积小于K的子数组 给定一个正整数数组 nums. 找出该数组内乘积小于 k 的连续的子数组的个数. 示例 1: 输入: nums = [10,5,2,6], k = 100 输出: 8 解 ...
- (剑指Offer)面试题31:连续子数组的最大和
题目: 输入一个整型数组,数组里有正数也有负数,数组中一个或连续多个整数组成一个子数组,求所有子数组的和的最大值.要求时间复杂度为O(n) 思路: 1.数组累加 从头到尾逐个累加数组中的每个数字,当累 ...
随机推荐
- VIew中的触摸事件 touchBegin 等一系列方法
5.触摸事件 touchBegin 等一系列方法 1)手指按下 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 2 ...
- iOS数据持久化(三)
#pragma mark - Core Data stack /** * @synthesize 关联成员变量和属性 */ @synthesize managedObjectContext = _ma ...
- .net framework缓存遍历
背景: 公司的老框架里的登录信息用的MemoryCache保存的,为了实现单用户登录(即一个账号不能同事登录),需要在登录前对已经登录的信息做遍历. 大致思路如下: 本方法可用于清除所有的缓存. 1. ...
- mysql之触发器before和after的区别(2)
我们先做个测试: 接上篇日志建的商品表g和订单表o和触发器 假设:假设商品表有商品1,数量是10: 我们往订单表插入一条记录: insert into o(gid,much) values(1,20) ...
- H.264的一些资料整理
本文转载自 http://blog.csdn.net/ljzcom/article/details/7258978, 如有需要,请移步查看. Technorati 标签: H.264 资料整理 --- ...
- linux下别名alias的设置
我有一个常用目录/volumes/mac/www’,每次都要输入这么长的路径,麻烦,所以有了以下配置 1.vi ~/.bash_profile 2.按住shift + i进入编辑状态 3.插入 ali ...
- 运行yum报错Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
今天给Centos通过rpm -Uvh装了个epel的扩展后,执行yum就开始报错: Error: Cannot retrieve metalink for repository: epel. Ple ...
- Stanford parser学习:LexicalizedParser类分析
上次(http://www.cnblogs.com/stGeekpower/p/3457746.html)主要是对应于javadoc写了下LexicalizedParser类main函数的功能,这次看 ...
- jquery如何通过name名称获取当前name的value值
本文为大家介绍下jquery通过name名称获取当前name的value值的具体实现,感兴趣的朋友可以参考下. 复制代码代码如下: $("*[name='name']").val( ...
- Delphi ISO 收藏!
CodeGear RAD Studio 2007 最终版(With Update4) v11.0.2902.10471http://altd.codegear.com/download/radstud ...