Skip to content

Commit

Permalink
leetcode: Add 1004
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Sep 24, 2024
1 parent e820fdb commit bc7f0b3
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/leetcode/1004.max-consecutive-ones-iii/Cargo.toml
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]
16 changes: 16 additions & 0 deletions src/leetcode/1004.max-consecutive-ones-iii/index.md
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}}
```
63 changes: 63 additions & 0 deletions src/leetcode/1004.max-consecutive-ones-iii/src/main.rs
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);
}
}
8 changes: 5 additions & 3 deletions src/leetcode/tags/array/sliding-window.md
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)

0 comments on commit bc7f0b3

Please sign in to comment.