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.

分析:用两个指针: start, end. 不断往前移动end,当arr[end]是0的话,就增加count的个数。如果count的个数超过k,那么就往前移动start, 然后不断比较global_max和 end - start + 1.
 1 class Solution {
 2     public int longestOnes(int[] A, int K) {
 3         int zeroCount = 0, start = 0, res = 0;
 4         for (int end = 0; end < A.length; end++) {
 5             if (A[end] == 0) {
 6                 zeroCount++;
 7             }
 8             while (zeroCount > K) {
 9                 if (A[start] == 0) {
10                     zeroCount--;
11                 }
12                 start++;
13             }
14             res = Math.max(res, end - start + 1);
15         }
16         return res;
17     }
18 }

原文地址:https://www.cnblogs.com/beiyeqingteng/p/12244468.html

时间: 2024-08-27 15:11:00

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

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

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

【Flume】【源码分析】flume中sink到hdfs,文件系统频繁产生文件,文件滚动配置不起作用? ERROR hdfs.BucketWriter: Hit max consecutive under-replication rotations (30)

[转载] http://blog.csdn.net/simonchi/article/details/43231891 ERROR hdfs.BucketWriter: Hit max consecutive under-replication rotations (30) 本人在测试hdfs的sink,发现sink端的文件滚动配置项起不到任何作用,配置如下: [plain] view plain copy print? a1.sinks.k1.type=hdfs a1.sinks.k1.cha

485. Max Consecutive Ones最长的连续1的个数

[抄题]: 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 of consecutive 1s is 3. [

【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_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

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 of consecutive 1s is 3. Note: T