当前位置:网站首页>487. number of maximum consecutive 1 II ●●
487. number of maximum consecutive 1 II ●●
2022-06-24 08:19:00 【chenyfan_】
487. Maximum continuous 1 The number of II ●●
describe
Given a binary array , You can at most 1 individual 0 Flip to 1, Find the largest one of them 1 The number of .
Example
Input :[1,0,1,1,0]
Output :4
explain : Flip the first 0 You can get the longest continuous 1.
When flipped , Maximum continuous 1 The number of 4.
Answer key
1. Dynamic programming
- dp[i][0] Express Subscript i-1 At the end , The longest continuous... Without a substitute operation 1 length ;
dp[i][1] Express Subscript i-1 At the end , The longest continuous... With a substitution operation 1 length ; - Initialize to 0;
- When nums[i-1] == 1:
continuity 1 Without interruption ,dp[i][0] = dp[i-1][0] + 1;,dp[i][1] = dp[i-1][1] + 1;
When nums[i-1] == 0:
Without replacement , continuity 1 interrupt ,dp[i][0] = 0;;
If you choose to replace , Is transferred from the unreplaced array + 1dp[i][1] = dp[i-1][0] + 1; - Traversing from front to back .
greedy : The continuous length of the operation used must be larger .
- Time complexity : O ( N ) O(N) O(N)
- Spatial complexity : O ( N ) O(N) O(N), You can optimize with a one-dimensional rolling array
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int findMaxConsecutiveOnes(vector<int>& nums){
int n = nums.size();
int ans = 0;
vector<vector<int>> dp(n+1, vector<int>(2, 0));
for(int i = 1; i <= n; ++i){
if(nums[i-1] == 1){
// dp[i] Corresponding nums[i-1]
dp[i][0] = dp[i-1][0] + 1; // Not replaced
dp[i][1] = dp[i-1][1] + 1; // Replaced
}else{
dp[i][0] = 0; // Do not replace , continuity 1 interrupt
dp[i][1] = dp[i-1][0] + 1; // Transfer... From an array that has never been replaced + 1
}
ans = max(ans, dp[i][1]); // greedy : The continuous length of the operation used must be larger
}
return ans;
}
int main(){
vector<int> nums = {
1, 0, 1, 1, 0};
cout << findMaxConsecutiveOnes(nums);
return 0;
}
2. The sliding window - Double pointer
In the statistics window 0 The number of , Greater than 1 when , Move the left pointer , Then determine the window size .
Reference resources 1004. Maximum continuous 1 The number of III ●●
- Time complexity : O ( N ) O(N) O(N)
- Spatial complexity : O ( 1 ) O(1) O(1)
int findMaxConsecutiveOnes2(vector<int>& nums){
int n = nums.size();
int ans = 0, l = 0, zero = 0;
for(int r = 0; r < n; ++r){
if(nums[r] == 0) ++zero; // Statistics 0 The number of
while(zero > 1){
if(nums[l] == 0) --zero; // Move the left pointer to the right
++l;
}
ans = max(ans, r-l+1);
}
return ans;
}
边栏推荐
- Catégorie de prêt 5
- Model effect optimization, try a variety of cross validation methods (system operation)
- Industrial computer anti cracking
- Search and recommend those things
- VsCode主题推荐
- Examples of corpus data processing cases (reading multiple text files, reading multiple files specified under a folder, decoding errors, reading multiple subfolder text, batch renaming of multiple fil
- Review of postgraduate English final exam
- June 27, 2021: given a positive array arr, it represents the weight of several people
- Swift extension chainlayout (UI chain layout) (source code)
- Leetcode 207: course schedule (topological sorting determines whether the loop is formed)
猜你喜欢

Shader common functions

疫情下更合适的开发模式

2021-03-16 COMP9021第九节课笔记

Utilisation de la fermeture / bloc de base SWIFT (source)

有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题

GraphMAE----论文快速阅读
![Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row](/img/16/011ba3aef1315c39526daac7e3ec89.png)
Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row

模型效果优化,试一下多种交叉验证的方法(系统实操)

Model effect optimization, try a variety of cross validation methods (system operation)

Screenshot recommendation - snipaste
随机推荐
C language_ Love and hate between string and pointer
[introduction to point cloud dataset]
问题3 — messageBox弹框,修改默认背景色
C语言_字符串与指针的爱恨情仇
Nodejs redlock notes
Industrial computer anti cracking
Four models of iPhone 13 series have been exposed, and indeed, they are 13 fragrant!
Simple summary of lighting usage
Selenium IDE的安装以及使用
Future trends in automated testing
June 27, 2021: given a positive array arr, it represents the weight of several people
Learning event binding of 3D visualization from scratch
[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA
疫情下更合适的开发模式
Dart development server, do I have a fever?
2021-03-11 COMP9021第八节课笔记
2021-03-09 COMP9021第七节课笔记
Utilisation de la fermeture / bloc de base SWIFT (source)
Transformers pretrainedtokenizer class
Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)