当前位置:网站首页>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;
}
边栏推荐
- 软件过程与项目管理期末复习与重点
- Getting started with ffmpeg
- 2021-03-11 COMP9021第八节课笔记
- 2022年制冷与空调设备运行操作上岗证题库及模拟考试
- Decltype usage introduction
- 宝塔面板安装php7.2安装phalcon3.3.2
- Catégorie de prêt 5
- Swift Extension ChainLayout(UI的链式布局)(源码)
- Four models of iPhone 13 series have been exposed, and indeed, they are 13 fragrant!
- WCF TCP protocol transmission
猜你喜欢

Swift Extension NetworkUtil(网络监听)(源码)

2022 PMP project management examination agile knowledge points (1)

Swift 基础 Swift才有的特性

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

JDBC 在性能测试中的应用

Selenium IDE的安装以及使用

2022年制冷与空调设备运行操作上岗证题库及模拟考试

李白最经典的20首诗排行榜
![[nilm] non intrusive load decomposition module nilmtk installation tutorial](/img/d0/bc5ea1cbca9ee96a2fe168484ffec4.png)
[nilm] non intrusive load decomposition module nilmtk installation tutorial

首次曝光 唯一全域最高等级背后的阿里云云原生安全全景图
随机推荐
Selenium IDE的安装以及使用
Solve the problem of notebook keyboard disabling failure
Pipeline concept of graphic technology
The applet reads more than 20 data, and the cloud function reads more than 100 restrictions
Simple summary of lighting usage
GraphMAE----论文快速阅读
Online education fades
Learning event binding of 3D visualization from scratch
Installation and use of selenium IDE
软件工程导论——第三章——需求分析
Graphmae ---- quick reading of papers
Blue Bridge Cup_ Queen n problem
About the iframe anchor, the anchor is offset up and down, and the anchor has page display problems Srcdoc problem of iframe
Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)
SQL intra statement operation
pyQt 常用系统的事件
auto使用示例
Ad-gcl:advantageous graph augmentation to improve graph contractual learning
小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现
QOpenGL显示点云文件