1004. Max Consecutive Ones III Medium
1/**
2 * [1004] Max Consecutive Ones III
3 *
4 * Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.
5 *
6 * Example 1:
7 *
8 * Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
9 * Output: 6
10 * Explanation: [1,1,1,0,0,<u>1,1,1,1,1,1</u>]
11 * Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
12 * Example 2:
13 *
14 * Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
15 * Output: 10
16 * Explanation: [0,0,<u>1,1,1,1,1,1,1,1,1,1</u>,0,0,0,1,1,1,1]
17 * Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
18 *
19 *
20 * Constraints:
21 *
22 * 1 <= nums.length <= 10^5
23 * nums[i] is either 0 or 1.
24 * 0 <= k <= nums.length
25 *
26 */
27pub struct Solution {}
28
29// problem: https://leetcode.com/problems/max-consecutive-ones-iii/
30// discuss: https://leetcode.com/problems/max-consecutive-ones-iii/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34impl Solution {
35 pub fn longest_ones(nums: Vec<i32>, k: i32) -> i32 {
36 0
37 }
38}
39
40// submission codes end
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn test_1004() {
48 }
49}
50
Back
© 2025 bowen.ge All Rights Reserved.