当前位置:网站首页>487. 最大连续1的个数 II ●●
487. 最大连续1的个数 II ●●
2022-06-24 06:57:00 【chenyfan_】
487. 最大连续1的个数 II ●●
描述
给定一个二进制数组,你可以最多将 1 个 0 翻转为 1,找出其中最大连续 1 的个数。
示例
输入:[1,0,1,1,0]
输出:4
解释:翻转第一个 0 可以得到最长的连续 1。
当翻转以后,最大连续 1 的个数为 4。
题解
1. 动态规划
- dp[i][0]表示 下标 i-1 结尾时,未使用替换操作的最长连续1长度;
dp[i][1]表示 下标 i-1 结尾时,使用了替换操作的最长连续1长度; - 初始化为0;
- 当 nums[i-1] == 1:
连续1不中断,dp[i][0] = dp[i-1][0] + 1;,dp[i][1] = dp[i-1][1] + 1;
当 nums[i-1] == 0:
不替换的话,连续1中断,dp[i][0] = 0;;
若选择替换,则从未替换的数组中转移 + 1dp[i][1] = dp[i-1][0] + 1; - 从前往后遍历。
贪心:使用了操作的连续长度肯定更大。
- 时间复杂度: O ( N ) O(N) O(N)
- 空间复杂度: O ( N ) O(N) O(N),可以用一维滚动数组优化
#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] 对应 nums[i-1]
dp[i][0] = dp[i-1][0] + 1; // 未替换过
dp[i][1] = dp[i-1][1] + 1; // 替换过
}else{
dp[i][0] = 0; // 不替换,连续1中断
dp[i][1] = dp[i-1][0] + 1; // 从未替换的数组中转移 + 1
}
ans = max(ans, dp[i][1]); // 贪心:使用了操作的连续长度肯定更大
}
return ans;
}
int main(){
vector<int> nums = {
1, 0, 1, 1, 0};
cout << findMaxConsecutiveOnes(nums);
return 0;
}
2. 滑动窗口 - 双指针
统计窗口中 0 的个数,大于 1 时,则移动左指针,然后判断窗口大小。
- 时间复杂度: O ( N ) O(N) O(N)
- 空间复杂度: 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; // 统计 0 的个数
while(zero > 1){
if(nums[l] == 0) --zero; // 左指针右移
++l;
}
ans = max(ans, r-l+1);
}
return ans;
}
边栏推荐
- OC extension detects whether an app is installed on the mobile phone (source code)
- Swift foundation features unique to swift
- Application of JDBC in performance test
- Using kubeconfig files to organize cluster access
- Opening chapter of online document technology - rich text editor
- [C language] system date & time
- Four models of iPhone 13 series have been exposed, and indeed, they are 13 fragrant!
- Dart development server, do I have a fever?
- Gossip: what happened to 3aC?
- Introduction to software engineering - Chapter 3 - Requirements Analysis
猜你喜欢

Latest news of awtk: new usage of grid control

Introduction to software engineering - Chapter 2 - feasibility study

直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)

Practice of opengauss database on CentOS, configuration

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

一文理解同步FIFO

C语言_字符串与指针的爱恨情仇

快速读论文----AD-GCL:Adversarial Graph Augmentation to Improve Graph Contrastive Learning

搜索与推荐那些事儿

Echart's experience (I): about y axis yaxis attribute
随机推荐
Synchronous FIFO
Swift extension chainlayout (UI chain layout) (source code)
OC extension detects whether an app is installed on the mobile phone (source code)
Blue Bridge Cup_ Queen n problem
Swift Extension NetworkUtil(網絡監聽)(源碼)
Methods of vector operation and coordinate transformation
1279_ Vsock installation failure resolution when VMware player installs VMware Tools
Teach you how to use the reflect package to parse the structure of go - step 1: parameter type check
51 single chip microcomputer_ External interrupt and timer / Counter interrupt
直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)
宝塔面板安装php7.2安装phalcon3.3.2
一文理解同步FIFO
JDBC 在性能测试中的应用
5g industrial router Gigabit high speed low delay
Model effect optimization, try a variety of cross validation methods (system operation)
Sql语句内运算问题
Solution of electric education system for intelligent supervision station
More appropriate development mode under epidemic situation
Unity culling related technologies
[C language] system date & time