LeetCode Learning Notes: Double Pointer (Updated)

The double pointer is an algorithm for titles on arrays, the main type of which is the sliding window.
Question Type 1: Sliding Window
Related topics: 3, 76, 209, 424, 438, 567, 713, 763, 845, 881, 904, 978, 992, 1004, 1040, 1052
Solution ideas: This type of problem is generally used to find the longest or shortest interval that meets the criteria. This interval can be represented by a window. A sliding window is to form a window with a left pointer and a right pointer. The window is moved by the pointer to form a qualified interval. Finally, the result interval is obtained.

During the exercise, I found a good template written by a big man:
Author: fuxuemingzhu
Links:https://leetcode-cn.com/problems/max-consecutive-ones-iii/solution/fen-xiang-hua-dong-chuang-kou-mo-ban-mia-f76z/
Source: LeetCode

def findSubArray(nums):
    N = len(nums) # Array/String Length
    left, right = 0, 0 # Double pointer, indicating the current traversed interval [left, right], closed interval
    sums = 0 # Used to count whether subarrays/intervals are valid, sums/counts may be changed depending on the title
    res = 0 # Save the maximum length of the subarray/substring that meets the title requirements
    while right < N: # When the right pointer does not search to the end of the array/string
        sums += nums[right] # Increase the sum/count of the number/character of the current right pointer
        while Section[left, right]Does not fit the title:# You need to move the left pointer until you find an area that fits the theme
            sums -= nums[left] # The sum/count of left position characters needs to be reduced from counter before moving the left pointer
            left += 1 # Really move the left pointer, note that you can't reverse the line above
        # By the end of while, we found a subarray/substring that matched the theme
        res = max(res, right - left + 1) # Results need to be updated
        right += 1 # Move the right pointer to explore new intervals
    return res

The left and right pointers are used in the sliding window. They move by dragging the left pointer forward, driven by the right pointer. The right pointer moves only one step at a time, while the left pointer may move more than one step at a time in the inner while cycle. The right pointer moves forward actively to explore unknown new areas; the left pointer is forced to move and is responsible for finding areas that satisfy the theme.
The overall idea of the template is:
Define two pointers, left and right, that point to the beginning and end of an interval, noting that the interval is closed; define sums to count the occurrences of each character within that interval;
The first while loop is to determine if the position of the right pointer exceeds the bounds of the array; each time the right reaches a new position, the sum/count of the right pointer needs to be increased;
The second while loop is to move the left pointer to the right where the [left, right] interval fits the theme; each time the left moves to a new location, the summation/count of the left pointer needs to be reduced;
After the second while cycle, a suitable [left, right] interval was found. The Title requires the maximum interval length, so res max(res, the length of the current interval) was updated.
Each time the right pointer moves one step to the right, it starts exploring new intervals.

Example:

//1052
//First count the number of satisfied customers and use the calm time of the boss as a window
//It then determines the number of satisfied customers plus the additional number of satisfied customers in the calm window of the boss
//Move the window continuously to get the maximum
class GrumpyBookstoreOwner {
    public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
    	//Left Pointer
    	int left = 0;
    	//Result
    	int result = 0;
    	//Determine the number of satisfied customers
    	int certain = 0;
    	//Statistics determine the number of satisfied customers
    	for(int i=0;i<customers.length;i++) {
    		if(grumpy[i] == 0) {
    			certain += customers[i];
    		}
    	}
    	//Use your boss's calm time as a window
    	for(int right=minutes-1;right<customers.length;right++) {
    		//Determine the number of satisfied customers plus the additional number of satisfied customers in the calm window of the boss
    		int temp = certain;
    		//Add the number of satisfied customers to the calm window of the boss
    		for(int i=left;i<=right;i++) {
    			if(grumpy[i] == 1) {
    				temp += customers[i];
    			}
    		}
    		left++;
    		result = Math.max(result, temp);
    	}
    	return result;
    }
}

One LeetCode question a day, rush!!!

Tags: Java Algorithm leetcode array

Posted on Sun, 26 Sep 2021 15:23:09 -0400 by Sgarissta