[滑动窗口] 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> zeros;
        int res = 0;
        for (int i = 0; i < A.size(); i++) {

            if (A[i] == 0) {
                zeros.push_back(i);
            }
            if (zeros.size() > K) {
                index = zeros.front();
                zeros.pop_front();
            }
            res = max(res, i - index);
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/fish1996/p/11287802.html

时间: 2024-10-24 21:49:52

[滑动窗口] leetcode 1004 Max Consecutive Ones III的相关文章

[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]

[滑动窗口] leetcode 424 Longest Repeating Character Replacement

problem:https://leetcode.com/problems/longest-repeating-character-replacement/ 维护一个最多包含k个额外字符的滑动窗口.需要记录当前出现次数最多字符的出现次数来判断窗口是否合法,如果超过了,就把首指针向后挪一位,同时更新最多出现次数.对每个合法窗口,取其中的最大值. class Solution { public: int characterReplacement(string s, int k) { int begi

滑动窗口 - leetcode

220. Contains Duplicate III //这里需要两个指针i和j,刚开始i和j都指向0,然后i开始向右走遍历数组,如果i和j之差大于k,且m中有nums[j],则删除并j加一.这样保证了m中所有的数的下标之差都不大于k,然后我们用map数据结构的lower_bound()函数来找一个特定范围,就是大于或等于nums[i] - t的地方,所有小于这个阈值的数和nums[i]的差的绝对值会大于t (可自行带数检验).然后检测后面的所有的数字,如果数的差的绝对值小于等于t,则返回tr

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]

滑动窗口-Moving Stones Until Consecutive II

2020-02-20 16:34:16 问题描述: 问题求解: public int[] numMovesStonesII(int[] stones) { int n = stones.length; Arrays.sort(stones); int min = n; int start = 0; for (int end = 0; end < n; end++) { while (stones[end] - stones[start] + 1 > n) start += 1; int cur

Leetcode 1052 Grumpy Bookstore Owner. (滑动窗口)

目录 问题描述 例子 方法 Leetcode 1052 问题描述 Today, the bookstore owner has a store open for customers.length minutes. Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute. On some minu

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]239. Sliding Window Maximum滑动窗口最大值

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding

leetcode的Hot100系列--3. 无重复字符的最长子串--滑动窗口

可以先想下这两个问题: 1.怎样使用滑动窗口? 2.如何快速的解决字符查重问题? 滑动窗口 可以想象一下有两个指针,一个叫begin,一个叫now 这两个指针就指定了当前正在比较无重复的字符串,当再往后读取一个字符的时候,就需要比较该字符在begin到now之间是否有重复,如果有重复的话,则记录当前字符串长度,然后把begin往后移动,继续寻找后面的无重复字符子串. 例如,这里的字符串是:"fabcade". 1.当开始比较字符串的时候,begin指向了第一个字符f,now也指向了第一