leetcode 1004. Maximum number of consecutive 1's III (high-quality solution)

Code:

class Solution {
    public int longestOnes(int[] nums, int k) {
        int length=nums.length;
        int zero=0; //计数器,计数翻转 0 的个数
        int max=0;  //记录当前获得的最长子数组长度
        for(int left=0,right=0;right<length;right++){
           if(nums[right]==0){
               zero++;
               while (zero>k){
                   if(nums[left]==0){
                       zero--;
                   }
                   left++;
               }
               //zero<=k
           }
           //nums[right]==1
            max=Math.max(max,right-left+1);
        }
        return max;
    }
}

answer:

        First of all, we can think about the meaning of this question. The question requires finding the maximum number of consecutive 1's in the array. Since continuous data is required, it means that we need to find a "subarray" of consecutive 1's, and there is a mechanism for flipping 0's. , so the subarray we found can accommodate up to k 0s 

        ​ ​ Then we can think of a violent solution to this problem, which is to traverse to obtain all sub-arrays, remove k sub-arrays containing more than 0, and find the longest sub-array among the remaining sub-arrays.

        Regarding the problem of subarrays and substrings, we can easily think of solving it throughsliding window - double pointer

        ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​        

        Point the pointers L and R to the position of subscript 0. The data between the L and R pointers is the subarray we want to discuss at this time. First, determine the data pointed to by the R pointer. The data is 1, so we do not need to Perform character flipping, record the length of the current subarray, and let R++ expand the length of the subarray we are discussing

1        1        1        0        0        0        1        1        1        1        0

R

        When the R pointer points to 1, we only need to record the length of the current subarray under the same conditions as above ( Compare the current length with the previously saved length, and save the larger one ), let R++ expand the length of the subarray we are discussing until the R pointer points to 0 

        At this time, 0 appears in the subarray we are discussing, but the question has a character flipping mechanism, so we can flip the currently encountered 0 into 1. We pass a Counter zero, records the number of 0s we have flipped, at this time zero = 1 (no cute little girl will really change the source data to 1),Only zero > k means that we can no longer flip. After flipping 0 to 1, we also record the length of the subarray currently discussed, and then let R++ continue to expand. The length of the subarray we are discussing 

1        1        1        0        0        0        1        1        1        1        0

                              R 

        When the R pointer points to the current position, the value in the counter zero is 3, which is already greater than k. Therefore, we cannot flip the 0 that the R pointer points to at this time. This also represents the longest length of the subarray with the L pointer as the first We have obtained, at this point we can let L++, discuss the maximum length of the subarray with the next element first

1        1        1        0        0        0        1        1        1        1        0

                                                   R    

        Now a question arises, do we need to return the R pointer to the position of the L pointer and start from the beginning to discuss the maximum length of the subarray? The answer is no, because the number of 0s increases when the R pointer reaches the current position, and the subarray does not meet the requirements. This also means that the data before the R pointer meets the requirements, even if we let the R pointer return to the L pointer The position of is discussed from the beginning. The R pointer will definitely move to the current position, so there is no need to let the R pointer go back. At this time, zero =3 does not meet the requirements. We let L++ until the requirements are met.

1        1        1        0        0        0        1        1        1        1        0

          L         

                                                   R

        When the L pointer moves to the current position, zero = 2 = k, which meets the requirements and records the length of the subarray currently discussed, so that R++ can continue to expand the length of the subarray we are discussing. 

1        1        1        0        0        0        1        1        1        1        0

                                        L         

                                                   R

        The next operation is to loop until the R pointer moves to the current position, R = nums.length, then the loop ends and the longest subarray length is obtained

1        1        1        0        0        0        1        1        1        1        0

                                                   L      

                                                                                                            R

おすすめ

転載: blog.csdn.net/q322359/article/details/134819261