返回介绍

solution / 1000-1099 / 1004.Max Consecutive Ones III / README_EN

发布于 2024-06-17 01:03:32 字数 5508 浏览 0 评论 0 收藏 0

1004. Max Consecutive Ones III

中文文档

Description

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.

 

Example 1:

Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output: 6
Explanation: [1,1,1,0,0,1,1,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.

Example 2:

Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
Output: 10
Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.

 

Constraints:

  • 1 <= nums.length <= 105
  • nums[i] is either 0 or 1.
  • 0 <= k <= nums.length

Solutions

Solution 1

class Solution:
  def longestOnes(self, nums: List[int], k: int) -> int:
    ans = 0
    cnt = j = 0
    for i, v in enumerate(nums):
      if v == 0:
        cnt += 1
      while cnt > k:
        if nums[j] == 0:
          cnt -= 1
        j += 1
      ans = max(ans, i - j + 1)
    return ans
class Solution {
  public int longestOnes(int[] nums, int k) {
    int j = 0, cnt = 0;
    int ans = 0;
    for (int i = 0; i < nums.length; ++i) {
      if (nums[i] == 0) {
        ++cnt;
      }
      while (cnt > k) {
        if (nums[j++] == 0) {
          --cnt;
        }
      }
      ans = Math.max(ans, i - j + 1);
    }
    return ans;
  }
}
class Solution {
public:
  int longestOnes(vector<int>& nums, int k) {
    int ans = 0;
    int cnt = 0, j = 0;
    for (int i = 0; i < nums.size(); ++i) {
      if (nums[i] == 0) {
        ++cnt;
      }
      while (cnt > k) {
        if (nums[j++] == 0) {
          --cnt;
        }
      }
      ans = max(ans, i - j + 1);
    }
    return ans;
  }
};
func longestOnes(nums []int, k int) int {
  ans := 0
  j, cnt := 0, 0
  for i, v := range nums {
    if v == 0 {
      cnt++
    }
    for cnt > k {
      if nums[j] == 0 {
        cnt--
      }
      j++
    }
    ans = max(ans, i-j+1)
  }
  return ans
}
function longestOnes(nums: number[], k: number): number {
  const n = nums.length;
  let l = 0;
  for (const num of nums) {
    if (num === 0) {
      k--;
    }
    if (k < 0 && nums[l++] === 0) {
      k++;
    }
  }
  return n - l;
}
impl Solution {
  pub fn longest_ones(nums: Vec<i32>, mut k: i32) -> i32 {
    let n = nums.len();
    let mut l = 0;
    for num in nums.iter() {
      if num == &0 {
        k -= 1;
      }
      if k < 0 {
        if nums[l] == 0 {
          k += 1;
        }
        l += 1;
      }
    }
    (n - l) as i32
  }
}

Solution 2

class Solution:
  def longestOnes(self, nums: List[int], k: int) -> int:
    l = r = -1
    while r < len(nums) - 1:
      r += 1
      if nums[r] == 0:
        k -= 1
      if k < 0:
        l += 1
        if nums[l] == 0:
          k += 1
    return r - l
class Solution {
  public int longestOnes(int[] nums, int k) {
    int l = 0, r = 0;
    while (r < nums.length) {
      if (nums[r++] == 0) {
        --k;
      }
      if (k < 0 && nums[l++] == 0) {
        ++k;
      }
    }
    return r - l;
  }
}
class Solution {
public:
  int longestOnes(vector<int>& nums, int k) {
    int l = 0, r = 0;
    while (r < nums.size()) {
      if (nums[r++] == 0) --k;
      if (k < 0 && nums[l++] == 0) ++k;
    }
    return r - l;
  }
};
func longestOnes(nums []int, k int) int {
  l, r := -1, -1
  for r < len(nums)-1 {
    r++
    if nums[r] == 0 {
      k--
    }
    if k < 0 {
      l++
      if nums[l] == 0 {
        k++
      }
    }
  }
  return r - l
}
function longestOnes(nums: number[], k: number): number {
  const n = nums.length;
  let l = 0;
  let res = k;
  const count = [0, 0];
  for (let r = 0; r < n; r++) {
    count[nums[r]]++;
    res = Math.max(res, r - l);
    while (count[0] > k) {
      count[nums[l]]--;
      l++;
    }
  }
  return Math.max(res, n - l);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文