From bc7f0b3240ee4e66b34e6e3b0ebf30c49331a157 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Tue, 24 Sep 2024 23:23:34 +0800 Subject: [PATCH] leetcode: Add 1004 --- src/SUMMARY.md | 1 + .../1004.max-consecutive-ones-iii/Cargo.toml | 7 +++ .../1004.max-consecutive-ones-iii/index.md | 16 +++++ .../1004.max-consecutive-ones-iii/src/main.rs | 63 +++++++++++++++++++ src/leetcode/tags/array/sliding-window.md | 8 ++- 5 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 src/leetcode/1004.max-consecutive-ones-iii/Cargo.toml create mode 100644 src/leetcode/1004.max-consecutive-ones-iii/index.md create mode 100644 src/leetcode/1004.max-consecutive-ones-iii/src/main.rs diff --git a/src/SUMMARY.md b/src/SUMMARY.md index d0c11c0f..5b58ab71 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -320,6 +320,7 @@ - [0925. 长按键入 Long Pressed Name](leetcode/0925.long-pressed-name/index.md) - [0977. 有序数组的平方 Squares of a Sorted Array](leetcode/0977.squares-of-a-sorted-array/index.md) - [1001-1100](leetcode/by-id/1001-1100.md) + - [1004. 最大连续1的个数 III Max Consecutive Ones III](leetcode/1004.max-consecutive-ones-iii/index.md) - [1101-1200](leetcode/by-id/1101-1200.md) - [1201-1300](leetcode/by-id/1201-1300.md) - [1301-1400](leetcode/by-id/1301-1400.md) diff --git a/src/leetcode/1004.max-consecutive-ones-iii/Cargo.toml b/src/leetcode/1004.max-consecutive-ones-iii/Cargo.toml new file mode 100644 index 00000000..a0260322 --- /dev/null +++ b/src/leetcode/1004.max-consecutive-ones-iii/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lc-1004-max-consecutive-ones-iii" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] diff --git a/src/leetcode/1004.max-consecutive-ones-iii/index.md b/src/leetcode/1004.max-consecutive-ones-iii/index.md new file mode 100644 index 00000000..8b01ed59 --- /dev/null +++ b/src/leetcode/1004.max-consecutive-ones-iii/index.md @@ -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}} +``` diff --git a/src/leetcode/1004.max-consecutive-ones-iii/src/main.rs b/src/leetcode/1004.max-consecutive-ones-iii/src/main.rs new file mode 100644 index 00000000..78c739f3 --- /dev/null +++ b/src/leetcode/1004.max-consecutive-ones-iii/src/main.rs @@ -0,0 +1,63 @@ +// Copyright (c) 2024 Xu Shaohua . 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, 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; + +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); + } +} diff --git a/src/leetcode/tags/array/sliding-window.md b/src/leetcode/tags/array/sliding-window.md index ecdf697d..6a36adb9 100644 --- a/src/leetcode/tags/array/sliding-window.md +++ b/src/leetcode/tags/array/sliding-window.md @@ -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) \ No newline at end of file +- [220. Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii)