Leetcode task03

1. Lookup table

1.1 What is a lookup table

A lookup table is a collection of data elements of the same type.
Consider the basic data structure, the lookup table can be divided into:

  • The first category: search for existence-set

Whether the element'a' exists, usually set: set. Set only stores the key, and does not need to correspond to its corresponding value. The keys in the set are not allowed to be repeated

  • The second category: Find the correspondence (key-value correspondence)-dict

The element'a' appears several times: dict -> dictionary. The keys in the dict are not allowed to be repeated

  • The third category: change the mapping relationship-map

By uniformly expressing the relational mapping of the original sequence as others, only the lookup operation is performed in the lookup table without changing the data elements in the table, and this type of lookup table is called a static lookup table; on the contrary, the lookup operation is performed in the lookup table. At the same time, the operation of inserting data or deleting data is performed, and this type of table is called a dynamic lookup table.

1.2 Algorithm application of lookup table

1.2.1 349 Intersection Of Two Arrays 1

Insert picture description here

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        nums1 = set(nums1)
        return set([i for i in nums2 if i in nums1])

Use sets to intersect:

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
    	return set(num1)&set(num2)

2. Collision pointer

2.1 Algorithm definition

The definition of collision pointer is a sorted array, there are two pointers, pointing to the left end and the right end respectively; we can compare with the target value and dynamically move the pointer and the right pointer to approach the target value .
Note that the collision pointer requires the array to be sorted.
The time complexity of the collision pointer method, for unordered lists, the complexity is greater than nlogn and less than n^2, which is better than the brute force solution. In some places, the complexity is considered as n(O(n)+O(nlogn)=O(n)), and I personally think it should be greater than nlogn.

2.2 Algorithm application

167 Sum of Two Numbers II-Input an ordered array The ordered array
Insert picture description here
is very suitable for collision pointer method


class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        low, high = 0, len(numbers) - 1
        while low < high:
            total = numbers[low] + numbers[high]
            if total == target:
                return [low + 1, high + 1]
            elif total < target:
                low += 1
            else:
                high -= 1

        return [-1, -1]

3. Sliding array

3.1 Algorithm introduction

We have studied computer networks and all know that in order to avoid congestion, there is a sliding window protocol to control the traffic during network transmission. The protocol allows the sender to send multiple data packets before stopping and waiting for confirmation. Since the sender does not have to stop and wait for confirmation every time a packet is sent, this protocol can speed up data transmission and improve network throughput. The sliding window algorithm we talked about today is a principle.

Sliding window is an abstract concept commonly used in array/string problems. The window is usually a collection of a series of elements defined by the start and end index in the array/string, namely [left, right) (left closed, right open). The sliding window is a window that can "slide" the two boundaries in a certain direction.

3.2 Algorithm application

220 Contains Dupliccate Ⅲ
Insert picture description here

class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        record = set()
        for i in range(len(nums)):
            if 查找的比v-t大的最小的元素 <= v+t:
                return True
            record.add(nums[i])
            if len(record) == k+1:
                record.remove(nums[i-k])
        return False

4 summary

The search algorithm, on the one hand, can use a specific data structure to achieve fast indexing and use space in exchange for time. On the other hand, it can use a similar backtracking method to continuously search for substructures that meet the requirements in an interval, and the substructure does not meet the requirements When, adjust the boundary and perform the next search.

Among them, collision pointer method, binary search is suitable for sorted array.

reference

0. DataWhale lookup
1. Lookup table
2. Collision pointer-common question type
3. Sliding window algorithm

Guess you like

Origin blog.csdn.net/hu_hao/article/details/108232111