当前位置:网站首页>342 · Valley Sequence
342 · Valley Sequence
2022-07-30 09:20:00 【yinhua405】
描述
给你一个长度为nn的序列,Lets you find a valley sequence in his subsequence,山谷序列定义为:
- 序列的长度为偶数.
- 假设子序列的长度为2n2n.则前nnThe number is strictly decreasing,后nnThe number is strictly increasing,并且第一段的最后一个元素和第二段的第一个元素相同,也是这段序列中的最小值.
Now I want you to find the longest length of all subsequences that satisfies the valley sequence rule?
背完这套刷题模板,真的不一样!
Ba Linghu Chong of Peking University15Summarized from the experience of brushing questions over the years《算法小抄模板Cheat Sheet》助你上岸!
微信添加【jiuzhang0607】备注【小抄】领取
- 1<=len(num)<=10001<=len(num)<=1000
- 1<=num[i]<=100001<=num[i]<=10000
样例
样例 1:
输入: num = [5,4,3,2,1,2,3,4,5]
输出: 8
样例解释:
最长山谷序列为[5,4,3,2,2,3,4,5]
样例 2:
输入: num = [1,2,3,4,5]
输出: 0
样例解释:
不存在山谷序列
int valley(vector<int> &num) {
// write your code here
// write your code here
int size = num.size();
int left = 0;
int right = size - 1;
vector<int>dpleft(size, 0);
vector<int>dpright(size, 0);
map<int, set<int>> mapSet; //num[i],i
int leftMax = 0;
int rightMax = 0;
mapSet[num[left]] = set<int>{ 0};
//Count decreasing quantities from left to right
for (int left = 1; left < size; left++)
{
auto it = mapSet.find(num[left]);
if (it != mapSet.end())
{
it->second.insert(left);
}
else
{
mapSet[num[left]] = set<int>{ left };
}
for (int i = left - 1; i >= 0; i--)
{
if (num[left] < num[i])
{
dpleft[left] = max(dpleft[left], dpleft[i] + 1);
if (dpleft[left] > leftMax)
{
leftMax = dpleft[left];
}
}
}
}
//Count decreasing quantities from right to left
for (int right = size - 2; right >= 0; right--)
{
for (int j = right + 1; j < size; j++)
{
if (num[right] < num[j])
{
dpright[right] = max(dpright[right], dpright[j] + 1);
if (dpright[left] > rightMax)
{
rightMax = dpright[left];
}
}
}
}
int maxRet = 0;
for (int i = 0; i < size; i++)
{
if (mapSet[num[i]].size() <= 1)
{
continue;
}
set<int> tmp = mapSet[num[i]];
for (auto it : tmp)
{
if (it <= i)
{
continue;
}
int minTmp = min(dpleft[i], dpright[it]) +1;
if (minTmp > maxRet)
{
maxRet = minTmp;
}
}
}
return maxRet*2;
}
边栏推荐
- hcip实验
- SQL row-column conversion
- TreeSet parsing
- 嘉为鲸翼·多云管理平台荣获信通院可信云技术服务最佳实践
- How to use Jmeter to carry out high concurrency in scenarios such as panic buying and seckill?
- tabindex attribute of input tag & tabindex attribute of a tag
- ACL 2022 | Introduce angular margin to construct comparative learning objectives and enhance text semantic discrimination ability
- leetcode经典问题——11.盛水最多的容器
- FPGA基础协议二:I2C读写E²PROM
- 剖析SGI STL空间配置器(_S_refill内存块填充函数)
猜你喜欢
随机推荐
基于JSP实现校园二手交易平台
Webview中的超链接点击到外部浏览器打开
FPGA基础协议二:I2C读写E²PROM
【三子棋】——玩家VS电脑(C语言实现)
风险登记册
基于SSM开发实现校园疫情防控管理系统
npm指令
C language classic practice questions (3) - "Hanoi Tower (Hanoi)"
函数(1)
Lenovo Notebook How to Change Windows 10 Boot Logo Icon
浅论各种调试接口(JTAG、SWD、RDI、Jlink、Ulink、STlink)的区别
用示波器揭示以太网传输机制
MagicDraw secondary development process
2022杭电多校第一场
typescript2-typescript为什么给js添加类型支持
七大排序之直接选择排序
嘉为鲸翼·多云管理平台荣获信通院可信云技术服务最佳实践
【无标题】
解构的运用
PCB板加工流程中哪些因素会影响到传输线阻抗







![[Unity]UI切换环形滚动效果](/img/8d/5d139369285f3c49e3695d96c81d60.png)

