当前位置:网站首页>Break algorithm --- map

Break algorithm --- map

2022-07-08 01:31:00 killingwill




Resource reference :
LeetCode
Chinese version :https://leetcode-cn.com/problemset/all/
English version :https://leetcode.com/problemset/all/

Related code implementation – python Language :
https://github.com/muxintong/break-algorithm/

LeetCode Problem solving summary
https://github.com/labuladong/fucking-algorithm
https://labuladong.gitee.io/algo/
https://grandyang.com/leetcode/1147/

Python3 file
https://docs.python.org/3/

data structure
https://www.runoob.com/data-structures/
Problem Solving with Algorithms and Data Structures using Python
https://runestone.academy/runestone/books/published/pythonds/index.html




One . Monotonic stack

Two . Union checking set

3、 ... and . The sliding window

Four . The prefix and & HASH

5、 ... and . Difference

6、 ... and . A topological sort

7、 ... and . character string

8、 ... and .BFS

Nine .DFS

Ten . Dynamic programming

11、 ... and . Greedy Algorithm

Twelve . Dictionary tree















One . Monotonic stack

1.1 Introduction to monotone stack

Monotonic stack :

  1. Definition : A stack in which elements are monotonically increasing or decreasing , Monotone stack can only operate at the top of the stack .
    Monotonic stack = monotonous + Stack , Therefore, it meets two characteristics at the same time : monotonicity 、 The characteristics of the stack .
    monotonicity : The data stored in the monotone stack is orderly ( Monotonically increasing or decreasing ).
    Stack : Last in, first out .
  2. Realization : The elements in a monotone stack are monotonous , Before the element is added to the stack , At the top of the stack, the elements that break the monotony of the stack are deleted .
    Monotonically decreasing stacks : Find the first element larger than the current element from the left
	stack<int> st;
	for ( Traverse the array )
	{
    
		if ( The stack is empty  ||  The stack top element is greater than or equal to the current comparison element )
		{
    
			 Push ;
		}
		else
		{
    
			while ( Stack is not empty.  &&  The stack top element is smaller than the current element )
			{
    
				 Stack top element out of stack ;
				 Update results ;
			}
			 Current data stack ;
		}
	}

The above code optimization merge :

stack<int> st;
for( Traversal array ){
    
	while( The current element  >  Top element of stack  &&  The stack is not empty ){
    
		 Bomb stack ;//catch it! Found the first element larger than the current element from the left !
		 Update result set ;
	}
	 The current element is put on the stack ;
}

Monotonically increasing stacks : Find the first element smaller than the current element from the left

stack<int> st;
for(int i=0; i<arr.length; i++){
    
	while( The current element  <  Top element of stack  &&  The stack is not empty ){
    
		 Bomb stack ;//catch it! Found the first element smaller than the current element from the left !
		 Update result set ;
	}
	 The current element is put on the stack ;
}
  1. Time complexity :O(n)
    linear , Each element is traversed once .
    Because it satisfies monotonicity and each number will only be put on the stack once , And they will never enter the stack again after they are out of the stack , So we can reduce the time complexity O(n) Solve some problems in the case of .
  2. application :
    - Monotonically increasing stack can be found from left first Elements smaller than the current number
    - Monotonic decreasing stack can be found from left first Elements larger than the current number
原网站

版权声明
本文为[killingwill]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/189/202207072325529636.html