当前位置:网站首页>[leetcode] number of maximum consecutive ones
[leetcode] number of maximum consecutive ones
2022-07-02 00:33:00 【Xiao Zhu, Xiao Zhu will never admit defeat】
leetcode Medium maximum continuous 1 A summary of several categories of topics .
List of articles
Maximum continuous 1 The number of Ⅰ
1. Title Description
leetcode Topic link :485. Maximum continuous 1 The number of
2. Thought analysis
Method 1 : One traverse
encounter nums[i] == 1
, be count++
, encounter nums[i] == 0
, be count=0
, Then update the maximum continuous 1 The number of .
Method 2 : Double pointer
Fixed left boundary , Then judge the right boundary , Update the difference between the maximum value and the right boundary minus the left boundary .
Method 3 : Dynamic programming
dp[i] Says to the first i Maximum continuity at the end of elements 1 The number of .
among : The double pointer method has the shortest running time .
3. Reference code
Method 1 : One traverse
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int res = 0, count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
count++;
} else {
count = 0;
}
res = Math.max(res, count);
}
return res;
}
}
Method 2 : Double pointer
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int res = 0;
for (int i = 0; i < nums.length; i++) {
int j = i;
while (j < nums.length && nums[j] == 1) {
j++;
}
res = Math.max(res, j - i);
i = j;
}
return res;
}
}
Method 3 : Dynamic programming
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int n = nums.length;
int[] dp = new int[n + 1];
int res = 0;
for (int i = 1; i <= n; i++) {
if (nums[i - 1] == 1) {
dp[i] = dp[i - 1] + 1;
res = Math.max(res, dp[i]);
}
}
return res;
}
}
Maximum continuous 1 The number of Ⅱ
1. Title Description
leetcode Topic link :Leetcode 487. Maximum continuous 1 The number of Ⅱ,plus Membership title , Look at the problem directly .
Given a binary array , You can at most 1 individual 0 Flip to 1, Find the largest one of them 1 The number of .
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.
2. Thought analysis
3. Reference code
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int left = 0, right = 0;
int res = 0, count = 0;
while (right < nums.length) {
if (nums[right++] == 0) {
count++;
}
while (count > 1) {
if (nums[left++] == 0) {
count--;
}
}
res = Math.max(res, right - left);
}
return res;
}
}
Maximum continuous 1 The number of Ⅲ
1. Title Description
leetcode Topic link :1004. Maximum continuous 1 The number of III
2. Thought analysis
Maximum continuous 1 The number of Ⅱ An advanced version of , Yes k individual 0 Flip to 1.
3. Reference code
边栏推荐
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- 记录一下大文件上传偶然成功偶然失败问题
- Linux CentOS7安装Oracle11g的超完美新手教程
- Mysql database driver (JDBC Driver) jar package download
- Practical calculation of the whole process of operational amplifier hysteresis comparator
- Database -- sqlserver details
- Use the htaccess file to prohibit the script execution permission in the directory
- Leetcode skimming: binary tree 02 (middle order traversal of binary tree)
- An intern's journey to cnosdb
- ERP项目施行计划的目的是什么?
猜你喜欢
Leetcode skimming: stack and queue 03 (valid parentheses)
Niuke - Practice 101 - reasoning clown
SQL Server 安裝指南
Take the enclave Park as a sample to see how Yuhua and Shaoshan play the song of Chang Zhu Tan integrated development
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
BPR (Bayesian personalized sorting)
Export default the exported object cannot be deconstructed, and module Differences between exports
Correlation - intra group correlation coefficient
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
Windows installation WSL (II)
随机推荐
求逆序数的三个方法
[template] adaptive Simpson integral
牛客-练习赛101-推理小丑
Which app is better and more secure for stock mobile account opening
Three methods of finding inverse numbers
How to open an account for individual stock speculation? Is it safe?
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
vue 强制清理浏览器缓存
Difficult to get up syndrome (bit by bit greed)
Use the htaccess file to prohibit the script execution permission in the directory
股票开户哪个证券公司比较安全
Qt5.12.9 migration tutorial based on Quanzhi H3
If the browser is accidentally closed, how does react cache the forms filled out by users?
Vue force cleaning browser cache
The difference between timer and scheduledthreadpoolexecutor
leetcode96不同的二叉搜索樹
Asp .NetCore 微信订阅号自动回复之文本篇
[embedded system course design] a single key controls the LED light
LeetCode 0241.为运算表达式设计优先级 - DFS
Download the online video m3u8 tutorial