当前位置:网站首页>978. 最长湍流子数组
978. 最长湍流子数组
2022-06-09 09:28:00 【Mr Gao】
978. 最长湍流子数组
给定一个整数数组 arr ,返回 arr 的 最大湍流子数组的长度 。
如果比较符号在子数组中的每个相邻元素对之间翻转,则该子数组是 湍流子数组 。
更正式地来说,当 arr 的子数组 A[i], A[i+1], …, A[j] 满足仅满足下列条件时,我们称其为湍流子数组:
若 i <= k < j :
当 k 为奇数时, A[k] > A[k+1],且
当 k 为偶数时,A[k] < A[k+1];
或 若 i <= k < j :
当 k 为偶数时,A[k] > A[k+1] ,且
当 k 为奇数时, A[k] < A[k+1]。
示例 1:
输入:arr = [9,4,2,10,7,8,8,1,9]
输出:5
解释:arr[1] > arr[2] < arr[3] > arr[4] < arr[5]
示例 2:
输入:arr = [4,8,12,16]
输出:2
示例 3:
输入:arr = [100]
输出:1
这题挺难的,但是我们慢慢来:
int maxTurbulenceSize(int* arr, int arrSize){
int i;
int maxo=1;
int maxe=1;
int anso=1;
for(i=1;i<arrSize;i=i+2){
if(i==arrSize-1){
if(arr[i]>arr[i-1]){
anso++;
}
}
if(i<arrSize-1)
if(arr[i]<=arr[i-1]&&arr[i]<=arr[i+1]){
if(anso>maxo){
maxo=anso;
}
anso=1;
continue;
}
if(i<arrSize-1)
if(arr[i]<=arr[i-1]&&arr[i]>arr[i+1]){
if(anso>maxo){
maxo=anso;
}
anso=2;
continue;
}
if(i<arrSize-1)
if(arr[i]>arr[i-1]&&arr[i]<=arr[i+1]){
anso=anso+1;
if(anso>maxo){
maxo=anso;
}
anso=1;
}
else{
anso=anso+2;
}
}
if(anso>maxo){
maxo=anso;
}
// printf("o %d ",maxo);
int anse=1;
for(i=1;i<arrSize;i=i+2){
// printf("%d anso %d ",arr[i],anse);
if(i==arrSize-1){
if(arr[i]<arr[i-1]){
anse++;
}
}
if(i<arrSize-1)
if(arr[i]>=arr[i-1]&&arr[i]>=arr[i+1]){
if(anse>maxe){
maxe=anse;
}
// printf("e %d ",maxe);
anse=1;
continue;
}
if(i<arrSize-1)
if(arr[i]>=arr[i-1]&&arr[i]<arr[i+1]){
if(anse>maxe){
maxe=anse;
}
// printf("e %d ",maxe);
anse=2;
continue;
}
if(i<arrSize-1)
if(arr[i]<arr[i-1]&&arr[i]>=arr[i+1]){
anse=anse+1;
if(anse>maxe){
maxe=anse;
}
// printf("e %d ",maxe);
anse=1;
}
else{
anse=anse+2;
}
}
if(anse>maxe){
maxe=anse;
}
// printf("e %d ",maxe);
if(maxe>maxo){
return maxe;
}
else{
return maxo;
}
}
边栏推荐
- redis info命令 memory内存信息说明
- 关于电脑网络浏览器没有网络,但是QQ和微信可以登录,解决浏览器网络问题
- Extensions attribute of TS generics
- Solve the apscheduler error: run time of job... Next run at:...) "was missed by
- 【genius_platform软件平台开发】第三十七讲:网卡混杂模式和原始套接字
- LeetCode_ Stack_ Difficulties_ 394. string decoding
- openstack详解(十七)——openstack Nova其他配置
- [brain opening] how can knowledge-based enterprises start their own businesses recruit talents?
- Openstack explanation (13) -- Grace keystone setup and startup
- [1 data collection] complete learning path of data crawler
猜你喜欢

Redis集群实例内存使用率飙升排查
![[technology, business and management] drama learning and Entrepreneurship: Silicon Valley, episode 1-2, season 6](/img/21/9569fb9a013da1476645bbfb0cf93c.png)
[technology, business and management] drama learning and Entrepreneurship: Silicon Valley, episode 1-2, season 6

openstack详解(十七)——openstack Nova其他配置

Machine learning notes - breast cancer classification using keras and deep learning

【genius_platform软件平台开发】第一万零一讲:电力项目dz产品windows环境vs2017编译遇到的报错汇总
![[linear algebra] understand positive definite matrix and semi positive definite matrix](/img/e0/fa9378a7d136f7d9a62ea3934e496d.png)
[linear algebra] understand positive definite matrix and semi positive definite matrix
![[brain opening] how can knowledge-based enterprises start their own businesses recruit talents?](/img/bf/9aa0df3d3cabb36030cdd6732cc67a.jpg)
[brain opening] how can knowledge-based enterprises start their own businesses recruit talents?

MSF module search details

MSF information collection based on TCP protocol

使用一条查询语句查询数据在0-60,60-80,80-100分数范围内的人数
随机推荐
[FAQ for novices on the road] how to use tensorflow to play deep learning?
Openstack explanation (16) -- openstack Nova installation and database configuration
【新手上路常见问答】平面设计的基本原则
LeetCode_ Monotone stack_ Medium_ 581. shortest unordered continuous subarray
Openstack explanation (XV) -- basic principle of openstack Nova node
长安链ChainMaker多机环境
n-grams语言模型——【torch学习笔记】
【新手上路常见问答】如何用TensorFlow玩转深度学习?
【图机器学习】启发式链路预测方法
Troubleshooting of soaring memory utilization of redis cluster instances
Openstack explanation (18) -- Nova service startup and service creation
There is no network for the computer web browser, but QQ and wechat can log in to solve the browser network problem
Postman 接口压力测试
[FAQ for novices on the road] about Internet of things design
LeetCode_排序_中等_406. 根据身高重建队列
Longest common subsequence and longest common substring
LeetCode_ Sort_ Medium_ 406. reconstruct the queue based on height
MSF实战——ms17-010漏洞的危害
MSF information collection based on TCP protocol
【实战技能】Google I/O 2022大会AI/ML给开发者的启发