当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Splunk tag 的利用场景

【Flask框架②】——第一个Flask项目

20个电路能懂5个以上,足以证明你在电子行业混过!

一文带你玩转offer-01

看完这100个客户需求,我终于知道企业文档管理的秘密

Circuit analysis: constant current source circuit composed of op amp and triode

剖析SGI STL空间配置器(_S_refill内存块填充函数)

C语言经典练习题(3)——“汉诺塔(Hanoi)“
![[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers](/img/ff/f3de4952d64afe36c515a2220bfe9d.png)
[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers

EMC过不了?都是PCB工程师的锅?
随机推荐
What convenience does the RFID fixed asset inventory system bring to enterprises?
积分专题笔记-与路径无关条件
注解开发相关
嘉为鲸翼·多云管理平台荣获信通院可信云技术服务最佳实践
test2
涛思 TDengine 2.6+优化参数
hicp第六天
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs
反射技巧让你的性能提升 N 倍
【WeChat Mini Program】Page Events
Field interpretation under "Surgical variables (RX SUMM-SURG OTH REG/DIS)" in SEER database
电源完整性基础知识
看完这100个客户需求,我终于知道企业文档管理的秘密
typescript5-编译和安装ts代码
js currying
[Unity]UI切换环形滚动效果
SQL window function
解构的运用
【愚公系列】2022年07月 Go教学课程 021-Go容器之切片操作
ant-design form表单校验upload组件(附个人封装的上传组件)