Skip to content

Commit f740049

Browse files
committed
修改单调栈和单调队列的一些命名细节
1 parent 2658156 commit f740049

File tree

11 files changed

+16
-12
lines changed

11 files changed

+16
-12
lines changed
File renamed without changes.

data_structure/MonotonicStack.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**Translator: [YourName](Anything you want to link)**
66

77
**Author: [labuladong](https://github.com/labuladong)**
8+
89
`Stack` is a very simple data structure. The logical sequence of first in and last out conforms to the characteristics of some problems, such as function call stack.
910

1011
`Monotonic stack` is actually a stack. It just uses some ingenious logic to keep the elements in the stack orderly (monotone increasing or monotone decreasing) after each new element putting into the stack.
@@ -15,15 +16,16 @@ First, explain the original problem of `Next Greater Number`: give you an array,
1516

1617
Give you an array `[2,1,2,4,3]`,and you return an array `[4,2,4,-1,-1]`.
1718

18-
**Explanation**
19+
### Explanation
20+
1921
The number that is larger than `2` after `the first 2` is `4`.The number that is larger than `1` after `the first 1` is `2`. The number that is larger than `2` after `the second 2` is `4`.There is no number that is larger than `4` after `the fourth`,so fill in `-1`.There is no number that is larger than `3` after `the third`,so fill in `-1`.
2022

2123
It's a good idea for the violent solution of this problem.It scans the back of each element to find the first larger element. But the time complexity of the violent solution is `O (n^2)`.
2224

2325
This problem can be thought abstractly: think of the elements in the array as people standing side by side, and the size of the elements as the height of an adult. These people stand in line before you. How to find the `Next Greater Number` of element `"2"`? Very simply, if you can see the element `"2"`, then the first person you can see behind him is the `Next Greater Number` of `"2"`. Because the element smaller than `"2"` is not tall enough and it is blocked by `"2"`,the first one not being blocked is the answer.
2426

2527

26-
![ink-image](../pictures/%E5%8D%95%E8%B0%83%E6%A0%88/1.png)
28+
![ink-image](../pictures/MonotonicStack/1.png)
2729

2830
This is a very understandable situation,huh? With this abstract scenario in mind, let's look at the code first.
2931

@@ -52,7 +54,8 @@ Now that you have mastered the technique of using monotonic stack, and take a si
5254
5355
Give you an array `T = [73, 74, 75, 71, 69, 72, 76, 73]`, which stores the weather temperature in recent days(Is it in teppanyaki? No, it's in Fahrenheit). You return an array to calculate: for each day, how many days do you have to wait for a warmer temperature;and if you can't wait for that day, fill in `0`.
5456
55-
**Example**
57+
### Example
58+
5659
Give you `T = [73, 74, 75, 71, 69, 72, 76, 73]`, and you return `[1, 1, 4, 2, 1, 1, 0, 0]`.
5760
**Explanation**
5861
The first day is 73 degrees Fahrenheit, and the next day is 74 degrees Fahrenheit, which is higher than 73 degrees Fahrenheit.So for the first day, you can wait for a warmer temperature just one day. The same goes for the latter.
@@ -82,7 +85,7 @@ It's also `Next Greater Number`. Now suppose the array given to you is a ring an
8285

8386
Give you an array `[2,1,2,4,3]`,and you return an array `[4,2,4,-1,4]`. With the ring attribute, the last element `3` goes around and finds the element `4` larger than itself.
8487

85-
![ink-image](../pictures/%E5%8D%95%E8%B0%83%E6%A0%88/2.png)
88+
![ink-image](../pictures/MonotonicStack/2.png)
8689

8790
First of all, the memory of the computer is linear, and there is no real ring array. However, we can simulate the effect of ring array. Generally, we use the `%` operator to calculate the modulus (remainder) to get the ring effect:
8891

@@ -99,7 +102,7 @@ Back to the problem of `Next Greater Number`. After adding the ring attribute, t
99102

100103
If we are clear about the problem, it will be half solved. We can think about like this: "Double" the original array,or in another word,to connect another original array at the back. In this way, according to the previous "height comparison" process, each element can not only compare with the elements on its right, but also the elements on its left.
101104

102-
![ink-image (2)](../pictures/%E5%8D%95%E8%B0%83%E6%A0%88/3.png)
105+
![ink-image (2)](../pictures/MonotonicStack/3.png)
103106

104107
How do you achieve it? Of course, you can construct this double length array and apply the algorithm template. However, instead of constructing a new array, we can use the technique of `circular array` to simulate. Just look at the code:
105108

data_structure/Monotonic queue.md renamed to data_structure/Monotonic_queue.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# special data structure: monotonic queue
2-
Author:labuladong https://github.com/labuladong
32

4-
Translator:warmingkkk https://github.com/warmingkkk
3+
**Author:[labuladong](https://github.com/labuladong)**
4+
5+
**Translator:[warmingkkk](https://github.com/warmingkkk)**
56

67
The previous article talked about a special data structure "monotonic stack"a type of problem "Next Greater Number" is solved. This article writes a similar data structure "monotonic queue".
78

89
Maybe you haven't heard of the name of this data structure. In fact, it is not difficult. It is a "queue", but it uses a clever method to make the elements in the queue monotonically increase (or decrease). What's the use of this data structure? Can solve a series of problems with sliding windows.
910

10-
See a LeetCode title,difficulty is hard:
11+
See a LeetCode title, 239 question,difficulty is hard:
1112

12-
![](../pictures/单调队列/title.png)
13+
![](../pictures/monotonic_queue/title.png)
1314

1415
### 1, build a problem solving framewor
1516

@@ -62,7 +63,7 @@ vector<int> maxSlidingWindow(vector<int>& nums, int k) {
6263
}
6364
```
6465
65-
![图示](../pictures/单调队列/1.png)
66+
![图示](../pictures/monotonic_queue/1.png)
6667
6768
The idea is simple, understand? Below we start the highlight, the implementation of monotonic queues.
6869
@@ -106,7 +107,7 @@ public:
106107
107108
As you can imagine, adding the size of the number represents the weight of the person, squashing the underweight in front, and stopping until it encounters a larger magnitude.
108109
109-
![](../pictures/单调队列/2.png)
110+
![](../pictures/monotonic_queue/2.png)
110111
111112
If every element is added like this, the size of the elements in the monotonic queue will eventually decrease in a monotonic order, so our max () API can be written like this:
112113
@@ -127,7 +128,7 @@ void pop(int n) {
127128
128129
The reason to judge `data.front () == n` is because the queue head element n we want to delete may have been" squashed ", so we don't need to delete it at this time:
129130
130-
![](../pictures/单调队列/3.png)
131+
![](../pictures/monotonic_queue/3.png)
131132
132133
At this point, the monotonous queue design is complete, look at the complete problem-solving code:
133134
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

pictures/monotonic_queue/title.png

68.4 KB
Loading

0 commit comments

Comments
 (0)