当前位置:网站首页>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;
}
边栏推荐
- os.path.join()使用过程中遇到的坑
- 基金的募集,交易与登记
- Serialization of unity
- Part 1: building OpenGL environment
- 4-操作列表(循环结构)
- 研究生英语期末考试复习
- [test development] first knowledge of software testing
- Getting started with crawler to giving up 06: crawler play Fund (with code)
- Opening chapter of online document technology - rich text editor
- Simple summary of lighting usage
猜你喜欢

2022 PMP project management examination agile knowledge points (1)

Echart 心得 (一): 有关Y轴yAxis属性

The monthly salary of two years after graduation is 36K. It's not difficult to say

Moonwell Artemis is now online moonbeam network

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

FPGA的虚拟时钟如何使用?

Echart's experience (I): about y axis yaxis attribute

Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV

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

Écouter le réseau d'extension SWIFT (source)
随机推荐
June 27, 2021: given a positive array arr, it represents the weight of several people
Shader common functions
Serialization of unity
From jsonpath and XPath to spl
How to use the virtual clock of FPGA?
Search and recommend those things
Moonwell Artemis is now online moonbeam network
研究生英语期末考试复习
In the post epidemic era, the home service robot industry has just set sail
3-列表简介
JDBC 在性能测试中的应用
QOpenGL显示点云文件
疫情下更合适的开发模式
Keep one decimal place and two decimal places
[ACNOI2022]做过也不会
2021-03-11 COMP9021第八节课笔记
3-list introduction
How does dating software cut your leeks
Écouter le réseau d'extension SWIFT (source)
Solution of electric education system for intelligent supervision station