The maximum and minimum values in the window, monotonic stack

"Starting from 0 to make LeetCode" window, monotonous stack

1. The window
Insert picture description here
is solved with a doubly linked list

2. Window application: generate an array of window maximum values
Insert picture description here
Insert picture description here
3. Window application: the maximum value minus the minimum value less than or equal to the number of sub-arrays of num
Insert picture description here
Note: the sub-arrays are continuous

Violent solution:
Insert picture description here
optimal solution:
general idea: expand to the right
Insert picture description here
Insert picture description here
Insert picture description here
4. monotonic stack

Time complexity: O(N)
to find the big, from the bottom of the stack to the top of the stack, from the big
to the small to find the small, from the bottom of the stack to the top of the stack, from small to big

Find out the projects spark, hadoop that are the nearest to the right and the nearest to the left is larger than this number
Insert picture description here
Insert picture description here
, build projects, and distributed computing

5. Monotonic stack application: MaxTree
Insert picture description here
large root heap to construct an array :
Time complexity: O(N)

Monotonic stack:
Insert picture description here
6. Monotonic stack application: find the size of the largest sub-matrix
Insert picture description here
Insert picture description here

Find the largest rectangle in the histogram

First settle the elements of the popped stack during the traversal, and then settle the remaining elements in the traversal stack.
Insert picture description here
For the original problem , you only need to call a histogram for each row. The whole process is over
Insert picture description here
7. Monotonic stack applications: craters, beacon, The logarithm of mutual seeing.
Insert picture description here
Insert picture description here
If each mountain is not equal in height, that is, there is no same value.
Insert picture description here
Proof:
Insert picture description here
Advanced: The same height appears. It is great to use monotonous stacks
Insert picture description here
. This question

  • Take the largest base, traverse, and settle the popped C k 2 + 2 k C_k^2+2kCk2+2 k
  • After traversing, finally settle the remaining in the stack
  1. The third from the bottom and above, the general formula remains unchanged C k 2 + 2 k C_k^2+2kCk2+2 k
  2. The second to last one, if the number of occurrences at the bottom is greater than or equal to 2, C k 2 + 2 k C_k^2+2kCk2+2 k , if it is 1,C k 2 + k C_k^2+kCk2+k
  3. The last one, C k 2 C_k^2Ck2
    a
    Processing circular arrays


Insert picture description here
Insert picture description here
Insert picture description here
Change the next index of the circular array
Insert picture description here
C times 2 C_{times}^2Ctimes2
Insert picture description here
Deal with the rest of the last stack
Insert picture description here

Guess you like

Origin blog.csdn.net/bosszhao20190517/article/details/107921728