Interview----最长连续乘积字串
题目描述:
给一个浮点数序列,取最大乘积连续子串的值,例如 -2.5,4,0,3,0.5,8,-1,则取出的最大乘积连续子串为3,0.5,8。
也就是说,上述数组中,3 0.5 8这3个数的乘积3*0.5*8=12是最大的,而且是连续的。
算法1:
首先,枚举的话,复杂度是 O(N^2)
算法2:O(N)
首先发现这个题与最长连续子数组的和非常类似。
考虑用 D.P. 来求解。
定义 max_vec[i] ----- 以 a[i] 结尾的子数组的最大乘积
min_vec[i] ------ 以 a[i] 结尾的子数组的最小乘积。
为什么要定义 min_vec 呢?? 稍微讲解。。
现在考虑,如何求解 max_vec[i]??
它的值有以下两种可能:
1. a[i]
2. a[i] * 【a[i] 前面连续的若干个元素的乘积】;
1> if a[i] >= 0,为了让乘积最大,我们取 a[i] * max_vec[i-1] 即可;
2> if a[i] < 0, 为了让乘积最大,我们希望 【a[i]前面连续个若干个元素的乘积】越小越好,因此取 a[i] * min_vec[i-1]
从上面的分析,已经证明了 问题的最优子结构,所以 D.P. 可行。
代码如下:
/**
* @file max-multiphy.cc
* @brief max multiphy of a vector
* @author shoulinjun@126.com
* @version 0.1.00
* @date 2014-03-26
*/ #include <iostream>
#include <vector>
using namespace std; double MaxMultiply(double *a, int n)
{
double result(a[0]);
vector<double> max_vec(n), min_vec(n);
max_vec[0] = a[0];
min_vec[0] = a[0]; for(int i=1; i<n; ++i)
{
max_vec[i] = max(a[i], max(a[i]*min_vec[i-1], a[i]*max_vec[i-1]));
min_vec[i] = min(a[i], min(a[i]*min_vec[i-1], a[i]*max_vec[i-1]));
result = max(result, max_vec[i]);
}
return result;
}
Interview----最长连续乘积字串的更多相关文章
- 返回字符串中最长连续相同字串的长度---正则实现与JavaScript实现
JavaScript 实现 let str = 'AAABBAACCAAAADDE' function continuousString(str) { let finalObj = {} let te ...
- 动态规划--求最大连续子数组的和(Python实现)&求解最大连续乘积字串(Python实现)
def MaxSum(self,array,n): sum=array[0] result=array[0] for i in range(0,n): if sum<0: sum=a[i] el ...
- 最长公共字串(LCS)最长连续公共字串(LCCS)
链接1:http://blog.csdn.net/x_xiaoge/article/details/7376220 链接2:http://blog.csdn.net/x_xiaoge/article/ ...
- leetcode-3 最长无重复字串
3. Longest Substring Without Repeating Characters 题面 Given a string, find the length of the longest ...
- 最长连续回文串(最优线性时间O(n))
转自:http://blog.csdn.net/hopeztm/article/details/7932245 Given a string S, find the longest palindrom ...
- 马拉车 o(n)(最长连续回文串) hdu 3068
#include<bits/stdc++.h> ; using namespace std; +]; string manacher(string ss) { string tt=&quo ...
- (字符串)最长公共字串(Longest-Common-SubString,LCS)
题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...
- 最长公共子序列(LCS)问题 Longest Common Subsequence 与最长公告字串 longest common substr
问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk ...
- URAL 1517 Freedom of Choice(后缀数组,最长公共字串)
题目 输出最长公共字串 #define maxn 200010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int cmp(int *r,int a,int b, ...
随机推荐
- selenium+python笔记8
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @desc: 定制浏览器 """ imp ...
- jQuery图表开源软件
jQuery图表插件 jQchart jQchart 是一个jQuery的插件,用来绘制图表的.支持各种形状的图表. 示例代码: == Mini sample == $('#canvasMyID'). ...
- ajax轮询session阻塞问题
近来读了几篇关于ASP.NET下Session机制的文章,结合自己的实际应用,有点感想: 在ASP.NET的Session的默认机制下,对同一个SessionID下的用户请求ASP.NE ...
- openCV1
openCV是一个提供有C++ , android , python的开源图像处理的类库 中文论坛的网址是http://www.opencv.org.cn/forum.php?mod=forumdis ...
- png-24在ie6中的几种透明方法
转载 http://www.cnblogs.com/jikey/archive/2013/03/13/2957168.html 由于游戏类官网在页面背景和装饰人物的设计上追求画丽且与游戏风格想匹配,这 ...
- 164. Maximum Gap *HARD* -- 无序数组找出排序后连续元素的最大间隔
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- iOS App Icon图标 尺寸规范
Commit to AppStore:1024*1024 //for App IconIcon-60@3x.png:180*180 //iPhone 6 Plus (@3x)Icon-60@2x.pn ...
- 一步一步配置NLB
废话不说,配置NLB需要准备以下环境: 1. 至少两个服务器,我的是windows server 2008 R2; 我的两个服务器名分别为NLB3和NLB2,其中NLB3是主,为什么呢?后面会谈到,在 ...
- myeclipse设置编码格式的4种情况
(1).设置myeclipse工作空间的编码格式,作用范围最大 window-->preference-->general-->workspace-->text file en ...
- linux 下echo命令写入文件内容
http://blog.csdn.net/xukai871105/article/details/35834703 echo "Raspberry" > test.txt