[LeetCode] 1004. Max Consecutive Ones III

Given an array A of 0s and 1s, we may change up to K values from 0 to 1.

Return the length of the longest (contiguous) subarray that contains only 1s.

Example 1:

Input: A = [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: A = [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.

Note:

  1. 1 <= A.length <= 20000
  2. 0 <= K <= A.length
  3. A[i] is 0 or 1

O(n) solution with two pointers to go through the input array. All subarrays of 1s must be contiguous is a hint that we should use two pointers, left for the start index, right for the end index of a subarrays of all 1s.

Algorithm: As long as there are still unchecked elements, do (a) if there are no change quote left and the current element is 0,  compute the current subarry of 1s‘ length and update the max length if needed, then move the left pointer to point to the next element of the first 0 that was changed to 1.  This means we just release one change quote so that the right pointer can move forward by 1. (b) if there are change quotes left and the current element is 0, simply decrement the quote by 1. (c) move forward right by 1. After we‘ve checked all elements in the while loop, don‘t forget to compute the length of subarray that ends at the last element.

Key notes:

in case a, there is no need to update the change quote K because we advance the left pointer to exclude the first 0 that was flipped and we also advance right to include the current 0.

class Solution {
    public int longestOnes(int[] A, int K) {
        int left = 0, right = 0, len = 0;
        while(right < A.length) {
            if(K == 0 && A[right] == 0) {
                len = Math.max(len, right - left);
                while(left <= right && A[left] != 0) {
                    left++;
                }
                left++;
            }
            else if(A[right] == 0){
                K--;
            }
            right++;
        }
        len = Math.max(len, right - left);
        return len;
    }
}

原文地址:https://www.cnblogs.com/lz87/p/10468613.html

时间: 2024-08-05 05:49:37

[LeetCode] 1004. Max Consecutive Ones III的相关文章

[滑动窗口] leetcode 1004 Max Consecutive Ones III

problem:https://leetcode.com/problems/max-consecutive-ones-iii/ 维护最多包含k个0的滑动窗口,一旦超过了k个0,把队首的0 pop出来.不断更新当前滑动窗口中的数据个数,并取最大值返回即可. class Solution { public: int longestOnes(vector<int>& A, int K) { int count = 0; int index = -1; deque<int> zer

Max Consecutive Ones III

Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s. Example 1: Input: A = [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]

LeetCode Max Consecutive Ones

原题链接在这里:https://leetcode.com/problems/max-consecutive-ones/ 题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are co

【leetcode】485. Max Consecutive Ones

problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { if(nums.empty()) return 0; int ans = 0, max = INT_MIN; for(int i=0; i<nums.size(); i++) { if(nums[i]==1) ans++; else if(nums

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

LeetCode_485. Max Consecutive Ones

485. Max Consecutive Ones Easy Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number

【leetcode】Max Points on a Line (python)

给定一个点,除该点之外的其他所有点中,与该点的关系要么是共线,要么就是共点,也就是两点重合. 共线有三种情况:水平共线,垂直共线,倾斜的共线.合并下这三种情况就是斜率存在的共线和斜率不存在的共线. 那么我们的任务就是针对每个点,找出与其共线的这些情况中,共线最多的点的个数. 注意:最终的结果别忘了加上共点的个数. class Solution: def maxPoints(self, points ): if len( points ) <= 1: return len( points ) ma

【LeetCode】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

485. Max Consecutive Ones (最大连续数) by Python

485. Max Consecutive Ones 题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number o