Algorithm small note [1]

Figure
1. Bipartite graph
Color all vertices with two colors, and the two vertices of any edge have different colors.
For example: actors and movies.

2. Fully weighted directed graph
We regard each currency as a vertex of the graph, and the exchange rate between currencies as a weighted directed edge, then the entire exchange rate market is a fully weighted directed graph.
Arbitrage: The ring formed by the exchange rate graph, the product of the weights on it is greater than 1, trading along this ring will make you empty-handed~

Dynamic programming: Dynamic programming, DP
solves complex problems by decomposing the original problem into relatively simple sub-problems.
Applicable to problems with overlapping subproblems and optimal substructure properties, the dynamic rule method takes much less time than the brute force solution.
Three traversal methods of final result position dp[m][n]
1. Forward traversal
for(int i=0;i<m;i++)
for(int j=0;j<n,j++)
//calculate dp [i][j]

2. Reverse traversal
for(int i=m-1;i>=0;i–)
for(int j=n-1;j>=0;j–)
//calculate dp[i][j]

3. Oblique traversal
for(int k=2;k<=n;k++)
for(int i=0;i<n-1;i++)
int j=k+i-1;
//calculate dp[i] [j]

For example: collect change.

Backtracking algorithm
def backtrack(…):
for selection in selection list
Multi-selection
backtrack(…)
Undo selection
For example: full arrangement, N queens.

The brute force solution stage of the dynamic specification is the backtracking algorithm. Some problems have overlapping sub-problems, which can be optimized with dp table or memorandum, and the recursive tree is greatly pruned to become dynamic programming.
Motion rules: state, selection, base case
backtracking: path, selection list, end condition

Binary search
int binary_search(int[] nums, int target){ int l=0,r=nums.Length-1; while(l<=r){ int m=l+(rl)/2; if(nums[m ]<target){ l=m+1; }else if(nums[m]>target){ r=m-1; }else if(nums[m]==target){ //1. Return directly return m ; //2. Lock left border r=m-1; //3. Lock right border l=m+1; } }















//1.直接返回
return -1;

//2.锁定左边界
if(l>=nums.Length || nums[l]!=target)
    return -1;
    
//3.锁定右边界
if(r<0 || nums[r]!=target)
    return -1;

}

Sliding window technique
1. Minimum substring problem (output: does not require continuity, find the smallest substring containing all letters)
Find the minimum substring containing all letters of t="ABC" in the string s="AD0BEC0DEBANC".
Returns an empty string if it does not exist; otherwise returns the smallest substring.
ps: Use the left and right pointers of double pointers, both l and r point to s[0] at the beginning, and regard the closed interval [l, r] as a "window"; 1. First, keep r++ to make the window contain t, if
not Then it does not exist; (whether there is a solution)
2. Then move the left pointer to the right (l++), when the interval [l, r] does not contain t, stop l, at this time the window [l-1, r] is this (optimization)
3. Then repeat steps 1 and 2 until the right pointer points to s[length-1], select the shortest length from the [l-1,r] window of each round, namely is the smallest substring; (choose the best among all solutions)

2. Find all the anagrams in the string (output: starting indices of substrings that are consecutive and contain all letters)
anagram-valued strings with the same letters but different permutations.
s="cbaebabacd", p="abc"
returns the starting index of the anagram substring: [0, 6] -> 0: cba 6: bac
ps: use the left and right pointers of double pointers, l and r are both at the beginning Point to s[0], rst=null;
1. If rst==p, record l, l=r, and clear rst;
2. At this time, rst is in a clear state, and l and r point to the same element:
if p does not contain s [r], then l++/r++;
if p contains s[r]:
2.1 If rst does not, r++, rst adds s[r] and performs step 1
2.1 If rst does, then l++/r++;
3. If r moves When the last element is reached, go to step 1;

3. The longest substring length s=“pwwkewabwimna” without repeating characters
, then the longest substring length is
? =0, le=0, re=0;
(le is the left pointer position of the longest substring, re is the right pointer position of the longest substring, the longest substring can be intercepted by these two indexes)
1. If the right If the pointer moves to the last element, return the length len;
2. If rst does not contain s[r], then add, len=rst.Length, le=l, re=r, r++;
//There is a problem with step 3
3. If If rst contains s[r], clear it, l++, if s[l]!=s[r], then rst=s[l...r], len=rst.Length, le=l, re=r, r++, execute steps 2,
otherwise the left pointer continues to move right until l==r and then execute step 2;

Guess you like

Origin blog.csdn.net/itsxwz/article/details/128043522