当前位置:网站首页>1493. the longest subarray with all 1 after deleting an element
1493. the longest subarray with all 1 after deleting an element
2022-06-11 09:14:00 【Mr Gao】
1493. After deleting an element, it's all 1 Longest subarray of
Give you a binary array nums , You need to delete an element from it .
Please delete the element in the result array , Returns the longest and contains only 1 The length of the non empty subarray of .
If no such subarray exists , Please return 0 .
Tips 1:
Input :nums = [1,1,0,1]
Output :3
explain : Delete position 2 After ,[1,1,1] contain 3 individual 1 .
Example 2:
Input :nums = [0,1,1,1,0,1,1,0,1]
Output :5
explain : Delete position 4 After the number of ,[0,1,1,1,1,1,0,1] Maximum length of 1 Subarray is [1,1,1,1,1] .
Example 3:
Input :nums = [1,1,1]
Output :2
explain : You have to delete an element .
This question is also simple , Let's just do it as usual , In fact, you need to traverse the question data . then , Every time I hit a zero , We all make an elimination judgment , Then we can find the optimal solution , The solution code is as follows :
int longestSubarray(int* nums, int numsSize){
int max=0;
int num=0;
int i;
int pre=0;
int r=0;
for(i=0;i<numsSize;i++){
if(nums[i]==1){
num++;
}
if(nums[i]==0){
r=1;
if(num+pre>max){
max=num+pre;
}
pre=num;
num=0;
}
}
if(nums[numsSize-1]==1){
if(num+pre>max){
max=num+pre;
}
}
if(r==0){
return num-1;
}
return max;
}
边栏推荐
猜你喜欢

Exclusive interview with PMC member Liu Yu: female leadership in Apache pulsar community

Shandong University project training (IV) -- wechat applet scans web QR code to realize web login

Machine learning notes - convolutional neural network memo list

C language printing diamond
![[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly](/img/c5/40ea5571f187e525b2310812ff2af8.png)
[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly

机器学习笔记 - 卷积神经网络备忘清单

报错RuntimeError: BlobReader error: The version of imported blob doesn‘t match graph_transformer

Pulsar job Plaza | Tencent, Huawei cloud, shrimp skin, Zhong'an insurance, streamnational and other hot jobs

OpenCV CEO教你用OAK(四):创建复杂的管道

报错Version mismatch between installed depthai lib and the required one by the scrip.
随机推荐
Why is it difficult to implement informatization in manufacturing industry?
2161. 根据给定数字划分数组
Importance of implementation of clothing ERP in the project
OpenCV CEO教你用OAK(五):基于OAK-D和DepthAI的反欺骗人脸识别系统
1721. 交换链表中的节点
Comment l'entreprise planifie - t - elle la mise en oeuvre?
CUMT学习日记——ucosII理论解析—任哲版教材
203. remove linked list elements
Comparison and introduction of OpenCV oak cameras
MSF给正常程序添加后门
【分享】企业如何进行施行规划?
2095. 删除链表的中间节点
682. baseball game
Fabric.js 動態設置字號大小
PCBA方案定制,开发腕式血压计方案
Some learning records I=
2130. maximum twin sum of linked list
Customize PCBA scheme and develop wrist sphygmomanometer scheme
20. valid brackets
C language printing diamond