Skip to content

Commit c91afbe

Browse files
committed
update stack cheatshet
1 parent 500b3da commit c91afbe

File tree

2 files changed

+128
-2
lines changed

2 files changed

+128
-2
lines changed

doc/cheatsheet/stack.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ for i, val in enumerate(len(tmp)):
4848
# ...
4949
```
5050

51+
```java
52+
// java
53+
// LC 739
54+
for (int j = 0; j < temperatures.length; j++){
55+
int x = temperatures[j];
56+
/**
57+
* NOTE !!!
58+
* 1) while loop
59+
* 2) stack is NOT empty
60+
* 3) cache temperature smaller than current temperature
61+
*
62+
* st.peek().get(0) is cached temperature
63+
*/
64+
while (!st.isEmpty() && st.peek().get(0) < x){
65+
/**
66+
* st.peek().get(1) is idx
67+
*
68+
*/
69+
nextGreater[st.peek().get(1)] = j - st.peek().get(1);
70+
st.pop();
71+
}
72+
```
73+
5174
## 1) General form
5275

5376
### 1-1) Basic OP
@@ -58,7 +81,110 @@ for i, val in enumerate(len(tmp)):
5881
- Stack isEmpty
5982
- Stack hasElement
6083

61-
### 1-1-2) next greater number
84+
85+
86+
### 1-1-2-1) next greater element
87+
88+
```java
89+
// java
90+
// LC 496
91+
// V0
92+
// IDEA : STACK
93+
// https://www.youtube.com/watch?v=68a1Dc_qVq4
94+
/** NOTE !!!
95+
*
96+
* nums1 is "sub set" of nums2,
97+
* so all elements in nums1 are in nums2 as well
98+
* and in order to find next greater element in nums1 reference nums2
99+
* -> ACTUALLY we only need to check nums2
100+
* -> then append result per element in nums1
101+
*/
102+
/**
103+
*
104+
* Example 1)
105+
*
106+
* nums1 = [4,1,2]
107+
* nums2 = [1,3,4,2]
108+
* x
109+
* x
110+
* x
111+
* x
112+
* st = [1]
113+
* st = [3] map : {1:3}
114+
* st = [4], map : {1:3, 3:4}
115+
* st = [], map : {1:3, 3:4}
116+
*
117+
* so, res = [-1, 3, -1]
118+
*
119+
*
120+
* Example 2)
121+
*
122+
* nums1 = [1,3,5,2,4]
123+
* nums2 = [6,5,4,3,2,1,7]
124+
* x
125+
* x
126+
* x
127+
* x
128+
* x
129+
* x
130+
* x
131+
* x
132+
*
133+
* st = [6], map :{}
134+
* st = [6,5], map :{}
135+
* ..
136+
*
137+
* st = [6,5,4,3,2,1], map = {}
138+
* st = [], map = {6:7, 5:7,4:7,3:7,2:7,1:7}
139+
*
140+
*/
141+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
142+
143+
if (nums1.length == 1 && nums2.length == 1){
144+
return new int[]{-1};
145+
}
146+
147+
/**
148+
* NOTE !!!
149+
* we use map " collect next greater element"
150+
* map definition : {element, next-greater-element}
151+
*/
152+
Map<Integer, Integer> map = new HashMap<>();
153+
Stack<Integer> st = new Stack<>();
154+
155+
for (int x : nums2){
156+
/**
157+
* NOTE !!!
158+
* 1) use while loop
159+
* 2) while stack is NOT null and stack "top" element is smaller than current element (x) is nums2
160+
*
161+
* -> found "next greater element", so update map
162+
*/
163+
while(!st.isEmpty() && st.peek() < x){
164+
int cur = st.pop();
165+
map.put(cur, x);
166+
}
167+
/** NOTE !!! if not feat above condition, we put element to stack */
168+
st.add(x);
169+
}
170+
171+
//System.out.println("map = " + map);
172+
int[] res = new int[nums1.length];
173+
// fill with -1 for element without next greater element
174+
Arrays.fill(res, -1);
175+
for (int j = 0; j < nums1.length; j++){
176+
if(map.containsKey(nums1[j])){
177+
res[j] = map.get(nums1[j]);
178+
}
179+
}
180+
181+
//System.out.println("res = " + res);
182+
return res;
183+
}
184+
```
185+
186+
187+
### 1-1-2-2) next greater element 2
62188

63189
```python
64190
# V0

leetcode_java/src/main/java/LeetCodeJava/DFS/SentenceSimilarity2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public boolean areSentencesSimilarTwo_1(
6767
*
6868
*
6969
*
70-
*
70+
*
7171
* Yes,
7272
* you can replace the bitwise operation n << 1
7373
* with a simple multiplication by 2.

0 commit comments

Comments
 (0)