-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "lc-1004-max-consecutive-ones-iii" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
# 1004. 最大连续1的个数 III Max Consecutive Ones III | ||
|
||
[问题描述](https://leetcode.com/problems/max-consecutive-ones-iii) | ||
|
||
## 滑动窗口 | ||
|
||
这个问题跟之前的胡杨林补种一样, 用滑动窗口来解决: | ||
- 窗口右侧经过0时, 计数加1 | ||
- 当窗口区间内的0的个数大于k 时, 把窗口左侧向右移, 直到窗口范围内的0的个数不大于k | ||
- 然后更新最大的连续为1的个数 | ||
|
||
|
||
```rust | ||
{{#include src/main.rs:5:37}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
// 滑动窗口 | ||
pub fn longest_ones1(nums: Vec<i32>, k: i32) -> i32 { | ||
// 窗口右侧经过的0 的个数, 减去窗口左侧经过的0的个数, 就是需要翻转为1的个数 | ||
|
||
let mut left = 0; | ||
let mut right = 0; | ||
let mut num_zero = 0; | ||
let k = k as usize; | ||
let mut longest_ones = 0; | ||
|
||
while right < nums.len() { | ||
// 需要翻转 | ||
if nums[right] == 0 { | ||
num_zero += 1; | ||
} | ||
// 保证最大翻转次数不大于 k | ||
while num_zero > k { | ||
// 窗口左侧右移 | ||
if nums[left] == 0 { | ||
num_zero -= 1; | ||
} | ||
left += 1; | ||
} | ||
|
||
// 注意边界情况. | ||
longest_ones = longest_ones.max(right - left + 1); | ||
|
||
// 窗口右侧向右移 | ||
right += 1; | ||
} | ||
|
||
longest_ones as i32 | ||
} | ||
|
||
pub type SolutionFn = fn(Vec<i32>, i32) -> i32; | ||
|
||
fn check_solution(func: SolutionFn) { | ||
let nums = vec![1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0]; | ||
let k = 2; | ||
assert_eq!(func(nums, k), 6); | ||
|
||
let nums = vec![0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1]; | ||
let k = 3; | ||
assert_eq!(func(nums, k), 10); | ||
} | ||
|
||
fn main() { | ||
check_solution(longest_ones1); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{check_solution, longest_ones1}; | ||
|
||
#[test] | ||
fn test_longest_ones1() { | ||
check_solution(longest_ones1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
# 滑动窗口相关的问题列表 | ||
|
||
## 简单 | ||
1. [0485. 最大连续1的个数 Max Consecutive Ones](../../0485.max-consecutive-ones/index.md) | ||
|
||
## 中等 | ||
|
||
1. [0485. 最大连续1的个数 Max Consecutive Ones](../../0485.max-consecutive-ones/index.md) | ||
1. [1004. 最大连续1的个数 III Max Consecutive Ones III](../../1004.max-consecutive-ones-iii/index.md) | ||
|
||
TODO: | ||
|
||
- [424. 替换后的最长重复字符](https://leetcode.com/problems/longest-repeating-character-replacement) | ||
- [1004. 最大连续1的个数 III ](https://leetcode.com/problems/max-consecutive-ones-iii) | ||
|
||
## 困难 | ||
|
||
- [220. Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii) | ||
- [220. Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii) |