当前位置:网站首页>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;
}
边栏推荐
- 根据网络上的视频的m3u8文件通过ffmpeg进行合成视频
- 普通token
- The applet reads more than 20 data, and the cloud function reads more than 100 restrictions
- decltype用法介绍
- C# Lambda
- More appropriate development mode under epidemic situation
- 新准则金融资产三分类:AMC、FVOCI和FVTPL
- Installation and use of selenium IDE
- MySQL source and target table row count check
- Introduction to software engineering - Chapter 3 - Requirements Analysis
猜你喜欢

Écouter le réseau d'extension SWIFT (source)

Coordinate transformation of graphic technology

问题3 — messageBox弹框,修改默认背景色

jwt(json web token)

2022 PMP project management examination agile knowledge points (1)

疫情下更合适的开发模式

Synchronous FIFO

问题4 — DatePicker日期选择器,2个日期选择器(开始、结束日期)的禁用

Graphmae - - lecture rapide des documents

Swift extension chainlayout (UI chain layout) (source code)
随机推荐
Synchronous FIFO
LINQ 查询(2)
你还只知道测试金字塔?
快速读论文----AD-GCL:Adversarial Graph Augmentation to Improve Graph Contrastive Learning
More appropriate development mode under epidemic situation
搜索与推荐那些事儿
OpenCV get(propId) 常用的值
transformers PreTrainedTokenizer类
Atguigu---15- built in instruction
Atguigu---16-custom instruction
Pagoda panel installation php7.2 installation phalcon3.3.2
Utilisation de la fermeture / bloc de base SWIFT (source)
Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row
到底哪一首才是唐诗第一?
Simple refraction effect
Auto usage example
js滚动div滚动条到底部
Swift foundation features unique to swift
Swift Extension NetworkUtil(网络监听)(源码)
Online education fades