当前位置:网站首页>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)
- Decltype usage introduction
- 51 single chip microcomputer_ External interrupt and timer / Counter interrupt
- Auto usage example
- LINQ query (2)
- Dart development server, do I have a fever?
- About the iframe anchor, the anchor is offset up and down, and the anchor has page display problems Srcdoc problem of iframe
- 2021-03-11 COMP9021第八节课笔记
- Live wire, neutral wire and ground wire. Do you know the function of these three wires?
- Synchronous FIFO
猜你喜欢
![[run the script framework in Django and store the data in the database]](/img/6b/052679e5468e5a90be5c4339183f43.png)
[run the script framework in Django and store the data in the database]

2021-03-11 COMP9021第八节课笔记

Utilisation de la fermeture / bloc de base SWIFT (source)
![[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA](/img/a0/f03b8d9c8f5e53078c38cce11f8ad3.png)
[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA

Coordinate transformation of graphic technology

2021-03-09 COMP9021第七节课笔记
![[nilm] non intrusive load decomposition module nilmtk installation tutorial](/img/d0/bc5ea1cbca9ee96a2fe168484ffec4.png)
[nilm] non intrusive load decomposition module nilmtk installation tutorial

FPGA的虚拟时钟如何使用?

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

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
随机推荐
Graphmae - - lecture rapide des documents
SVN实测常用操作-记录操作大全
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
[introduction to point cloud dataset]
3-list introduction
Synchronous FIFO
SCM stm32f103rb, BLDC DC motor controller design, schematic diagram, source code and circuit scheme
Introduction to software engineering - Chapter 3 - Requirements Analysis
From jsonpath and XPath to spl
[run the script framework in Django and store the data in the database]
Backup and restore SQL Server Databases locally
[data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board
More appropriate development mode under epidemic situation
Leetcode 174 Dungeon games (June 23, 2022)
不止于观测|阿里云可观测套件正式发布
Getting started with crawler to giving up 06: crawler play Fund (with code)
Écouter le réseau d'extension SWIFT (source)
Industrial computer anti cracking
Swift 基础 闭包/Block的使用(源码)
Interview tutorial - multi thread knowledge sorting