Skip to content

Commit b8e936a

Browse files
committed
增加解题范式,增加注释
1 parent 7cbc537 commit b8e936a

File tree

5 files changed

+246
-10
lines changed

5 files changed

+246
-10
lines changed

docs/0000-00-note.adoc

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,226 @@ image::images/quick-sort-01.gif[{image_attr}]
2121
== TODO
2222

2323
* [ ] https://leetcode.cn/problems/number-of-islands/solutions/211211/dao-yu-lei-wen-ti-de-tong-yong-jie-fa-dfs-bian-li-/[200. 岛屿数量 - 岛屿类问题的通用解法、DFS 遍历框架^]
24+
25+
== 解题范式
26+
27+
. Two Pointers
28+
.. Pair with Target Sum (easy)
29+
.. Find Non-Duplicate Number Instances (easy)
30+
.. Squaring a Sorted Array (easy)
31+
.. Triplet Sum to Zero (medium)
32+
.. Triplet Sum Close to Target (medium)
33+
.. Triplets with Smaller Sum (medium)
34+
.. Subarrays with Product Less than a Target (medium)
35+
.. Dutch National Flag Problem (medium)
36+
.. Problem Challenge 1: Quadruple Sum to Target (medium)
37+
.. Problem Challenge 2: Comparing Strings containing Backspaces (medium)
38+
.. Problem Challenge 3: Minimum Window Sort (medium)
39+
. Fast & Slow Pointers
40+
.. LinkedList Cycle (easy)
41+
.. Middle of the LinkedList (easy)
42+
.. Start of LinkedList Cycle (medium)
43+
.. Happy Number (medium)
44+
.. Problem Challenge 1: Palindrome LinkedList (medium)
45+
.. Problem Challenge 2: Rearrange a LinkedList (medium)
46+
.. Problem Challenge 3: Cycle in a Circular Array (hard)
47+
. Sliding Window
48+
.. Maximum Sum Subarray of Size K (easy)
49+
.. Smallest Subarray With a Greater Sum (easy)
50+
.. Longest Substring with K Distinct Characters (medium)
51+
.. Fruits into Baskets (medium)
52+
.. Longest Substring with Same Letters after Replacement (hard)
53+
.. Longest Subarray with Ones after Replacement (hard)
54+
.. Problem Challenge 1: Permutation in a String (hard)
55+
.. Problem Challenge 2: String Anagrams (hard)
56+
.. Problem Challenge 3: Smallest Window containing Substring (hard)
57+
.. Problem Challenge 4: Words Concatenation (hard)
58+
. Merge Intervals
59+
.. Merge Intervals (medium)
60+
.. Insert Interval (medium)
61+
.. Intervals Intersection (medium)
62+
.. Conflicting Appointments (medium)
63+
.. Problem Challenge 1: Minimum Meeting Rooms (hard)
64+
.. Problem Challenge 2: Maximum CPU Load (hard)
65+
.. Problem Challenge 3: Employee Free Time (hard)
66+
. Cyclic Sort
67+
.. Cyclic Sort (easy)
68+
.. Find the Missing Number (easy)
69+
.. Find all Missing Numbers (easy)
70+
.. Find the Duplicate Number (easy)
71+
.. Find all Duplicate Numbers (easy)
72+
.. Problem Challenge 1: Find the Corrupt Pair (easy)
73+
.. Problem Challenge 2: Find the Smallest Missing Positive Number (medium)
74+
.. Problem Challenge 3: Find the First K Missing Positive Numbers (hard)
75+
. In-place Reversal of a Linked List
76+
.. Reverse a LinkedList (easy)
77+
.. Reverse a Sub-list (medium)
78+
.. Reverse every K-element Sub-list (medium)
79+
.. Problem Challenge 1: Reverse alternating K-element Sub-list (medium)
80+
.. Problem Challenge 2: Rotate a LinkedList (medium)
81+
. Stacks
82+
.. Operations on Stack
83+
.. Implementing Stack Data Structure
84+
.. Applications of Stack
85+
.. Problem 1: Balanced Parentheses
86+
.. Problem 2: Reverse a String
87+
.. Problem 3: Decimal to Binary Conversion
88+
.. Problem 4: Next Greater Element
89+
.. Problem 5: Sorting a Stack
90+
.. Problem 6: Simplify Path
91+
. Monotonic Stack
92+
.. Remove Nodes From Linked List (medium)
93+
.. Remove All Adjacent Duplicates In String (easy)
94+
.. Next Greater Element (easy)
95+
.. Daily Temperatures (easy)
96+
.. Remove All Adjacent Duplicates in String II (medium)
97+
.. Remove K Digits (hard)
98+
. Hash Maps
99+
.. Introduction to Hash Tables
100+
.. Issues with Hash Tables
101+
.. Problem 1: First Non-repeating Character (easy)
102+
.. Problem 2: Largest Unique Number (easy)
103+
.. Problem 3: Maximum Number of Balloons (easy)
104+
.. Problem 4: Longest Palindrome(easy)
105+
.. Problem 5: Ransom Note (easy)
106+
. Tree Breadth First Search
107+
.. Binary Tree Level Order Traversal (easy)
108+
.. Reverse Level Order Traversal (easy)
109+
.. Zigzag Traversal (medium)
110+
.. Level Averages in a Binary Tree (easy)
111+
.. Minimum Depth of a Binary Tree (easy)
112+
.. Level Order Successor (easy)
113+
.. Connect Level Order Siblings (medium)
114+
.. Problem Challenge 1: Connect All Level Order Siblings (medium)
115+
.. Problem Challenge 2: Right View of a Binary Tree (easy)
116+
. Tree Depth First Search
117+
.. Binary Tree Path Sum (easy)
118+
.. All Paths for a Sum (medium)
119+
.. Sum of Path Numbers (medium)
120+
.. Path With Given Sequence (medium)
121+
.. Count Paths for a Sum (medium)
122+
.. Problem Challenge 1: Tree Diameter (medium)
123+
.. Problem Challenge 2: Path with Maximum Sum (hard)
124+
. Graphs
125+
.. Graph Representations
126+
.. Graph as an Abstract Data Type (ADT)
127+
.. Graph Traversal - Depth First Search(DFS)
128+
.. Graph Traversal - Breadth First Search (BFS)
129+
.. Problem 1: Find if Path Exists in Graph(easy)
130+
.. Problem 2: Number of Provinces (medium)
131+
.. Problem 3: Minimum Number of Vertices to Reach All Nodes(medium)
132+
. Island (Matrix Traversal)
133+
.. Number of Islands (easy)
134+
.. Biggest Island (easy)
135+
.. Flood Fill (easy)
136+
.. Number of Closed Islands (easy)
137+
.. Problem Challenge 1 (easy)
138+
.. Problem Challenge 2 (medium)
139+
.. Problem Challenge 3 (medium)
140+
. Two Heaps
141+
.. Find the Median of a Number Stream (medium)
142+
.. Sliding Window Median (hard)
143+
.. Maximize Capital (hard)
144+
.. Problem Challenge 1: Next Interval (hard)
145+
. Subsets
146+
.. Subsets (easy)
147+
.. Subsets With Duplicates (easy)
148+
.. Permutations (medium)
149+
.. String Permutations by changing case (medium)
150+
.. Balanced Parentheses (hard)
151+
.. Unique Generalized Abbreviations (hard)
152+
.. Problem Challenge 1: Evaluate Expression (hard)
153+
.. Problem Challenge 2: Structurally Unique Binary Search Trees (hard)
154+
.. Problem Challenge 3: Count of Structurally Unique Binary Search Trees (hard)
155+
. Modified Binary Search
156+
.. Order-agnostic Binary Search (easy)
157+
.. Ceiling of a Number (medium)
158+
.. Next Letter (medium)
159+
.. Number Range (medium)
160+
.. Search in a Sorted Infinite Array (medium)
161+
.. Minimum Difference Element (medium)
162+
.. Bitonic Array Maximum (easy)
163+
.. Problem Challenge 1: Search Bitonic Array (medium)
164+
.. Problem Challenge 2: Search in Rotated Array (medium)
165+
.. Problem Challenge 3: Rotation Count (medium)
166+
. Bitwise XOR
167+
.. Single Number (easy)
168+
.. Two Single Numbers (medium)
169+
.. Complement of Base 10 Number (medium)
170+
.. Problem Challenge 1: Flip and Invert an Image (hard)
171+
. Top 'K' Elements
172+
.. Top 'K' Numbers (easy)
173+
.. Kth Smallest Number (easy)
174+
.. 'K' Closest Points to the Origin (easy)
175+
.. Connect Ropes (easy)
176+
.. Top 'K' Frequent Numbers (medium)
177+
.. Frequency Sort (medium)
178+
.. Kth Largest Number in a Stream (medium)
179+
.. 'K' Closest Numbers (medium)
180+
.. Maximum Distinct Elements (medium)
181+
.. Sum of Elements (medium)
182+
.. Rearrange String (hard)
183+
.. Problem Challenge 1: Rearrange String K Distance Apart (hard)
184+
.. Problem Challenge 2: Scheduling Tasks (hard)
185+
.. Problem Challenge 3: Frequency Stack (hard)
186+
. Greedy Algorithms
187+
.. Valid Palindrome II (easy)
188+
.. Maximum Length of Pair Chain (medium)
189+
.. Minimum Add to Make Parentheses Valid (medium)
190+
.. Remove Duplicate Letters (medium)
191+
.. Largest Palindromic Number (medium)
192+
.. Removing Minimum and Maximum From Array (medium)
193+
. 0/1 Knapsack (Dynamic Programming)
194+
.. 0/1 Knapsack (medium)
195+
.. Equal Subset Sum Partition (medium)
196+
.. Subset Sum (medium)
197+
.. Minimum Subset Sum Difference (hard)
198+
.. Problem Challenge 1: Count of Subset Sum (hard)
199+
.. Problem Challenge 2: Target Sum (hard)
200+
. Backtracking
201+
.. Combination Sum (medium)
202+
.. Word Search (medium)
203+
.. Factor Combinations (medium)
204+
.. Split a String Into the Max Number of Unique Substrings (medium)
205+
.. Sudoku Solver (hard)
206+
. Trie
207+
.. Implement Trie (Prefix Tree) (medium)
208+
.. Index Pairs of a String (easy)
209+
.. Design Add and Search Words Data Structure (medium)
210+
.. Extra Characters in a String (medium)
211+
.. Search Suggestions System (medium)
212+
. Topological Sort (Graph)
213+
.. Topological Sort (medium)
214+
.. Tasks Scheduling (medium)
215+
.. Tasks Scheduling Order (medium)
216+
.. All Tasks Scheduling Orders (hard)
217+
.. Alien Dictionary (hard)
218+
.. Problem Challenge 1: Reconstructing a Sequence (hard)
219+
.. Problem Challenge 2: Minimum Height Trees (hard)
220+
. Union Find
221+
.. Redundant Connection (medium)
222+
.. Number of Provinces (medium)
223+
.. Is Graph Bipartite? (medium)
224+
.. Path With Minimum Effort (medium)
225+
. Ordered Set
226+
.. Merge Similar Items (easy)
227+
.. 132 Pattern (medium)
228+
.. My Calendar I (medium)
229+
.. Longest Continuous Subarray (medium)
230+
. Prefix Sum
231+
.. Find the Middle Index in Array (easy)
232+
.. Left and Right Sum Differences (easy)
233+
.. Maximum Size Subarray Sum Equals k (medium)
234+
.. Binary Subarrays With Sum (medium)
235+
.. Subarray Sums Divisible by K (medium)
236+
.. Sum of Absolute Differences in a Sorted Array (medium)
237+
.. Subarray Sum Equals K (medium)
238+
. Multi-threaded
239+
.. Same Tree (medium)
240+
.. Invert Binary Tree (medium)
241+
.. Binary Search Tree Iterator (medium)
242+
243+
244+
== 参考资料
245+
246+
. https://www.designgurus.io/course/grokking-the-coding-interview[Grokking the Coding Interview Patterns^]

docs/0000-23-monotonic-stack.adoc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,36 @@
55

66
一般会用到 `Deque` 的以下四个方法:
77

8-
* `deque.isEmpty()`:如果deque不包含任何元素,则返回true,否则返回false。因为要栈顶元素在满足要求的时候要弹出,所以需要进行空栈判断。有些场景,可能栈一定不会空的时候,就不需要该方法进行空栈判断。
9-
* `deque.push(e)`:将元素e入栈。一定能用到该方法
10-
* `deque.pop()`:将栈顶元素弹出,并返回当前弹出的栈顶元素。一定能用到该方法。
11-
* `deque.peek()`:获取栈顶元素,不弹出。一定能用到该方法。
8+
* `deque.isEmpty()`:如果 `deque` 不包含任何元素,则返回 `true`,否则返回 `false`。因为要栈顶元素在满足要求的时候要弹出,所以需要进行空栈判断。有些场景,可能栈一定不会空的时候,就不需要该方法进行空栈判断。
9+
* `deque.push(e)`:将元素 `e` 入栈
10+
* `deque.pop()`:将栈顶元素弹出,并返回当前弹出的栈顶元素。
11+
* `deque.peek()`:获取栈顶元素,不弹出。
1212

1313
[{java_src_attr}]
1414
----
15-
// 第一个一个单调栈
15+
// 定义一个单调栈
1616
Deque<Integer> stack = new LinkedList<>();
1717
// 第一个元素,直接添加
1818
stack.push(0); // 这里存的是数组下标
1919
for (int i = 1; i < nums.length; i++) {
20-
// 单调递减栈这里就是大于,即 nums[i] > nums[deque.peek()]
20+
// 单调递增栈这里就是大于,即 nums[i] > nums[deque.peek()]
2121
if (nums[i] < nums[stack.peek()]) {
2222
stack.push(i);
2323
} else if (nums[i] == nums[stack.peek()]) {
2424
stack.push(i);
25-
//此处除了入栈,在有些场景下,还有可能有其他操作
25+
// 此处除了入栈,在有些场景下,还有可能有其他操作
2626
// ..............
2727
} else {
28-
//循环比较,直到遇到当前元素小于栈顶的元素情况,跳出循环
29-
// 单调递减栈,这里是小于,即nums[i]<nums[deque.peek()]
28+
// 循环比较,直到遇到当前元素小于栈顶的元素情况,跳出循环
29+
// 单调递增栈,这里是小于,即nums[i] < nums[deque.peek()]
3030
while (!stack.isEmpty() && nums[i] > nums[stack.peek()]) {
3131
//主要逻辑
3232
// ............
3333
// ............
3434
// 弹出栈顶元素
3535
stack.pop();
36-
} stack.push(i);
36+
}
37+
stack.push(i);
3738
}
3839
}
3940
----

docs/0503-next-greater-element-ii.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ image::images/0503-01.png[{image_attr}]
2828

2929
image::images/0503-02.png[{image_attr}]
3030

31+
// TODO 图解是从前向后处理,代码时从后向前处理,尝试从前向后的处理方案。
32+
3133
[[src-0503]]
3234
[{java_src_attr}]
3335
----

docs/index.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,3 +2745,5 @@ include::1340-jump-game-v.adoc[leveloffset=+1]
27452745
include::1644-lowest-common-ancestor-of-a-binary-tree-ii.adoc[leveloffset=+1]
27462746

27472747
include::1905-count-sub-islands.adoc[leveloffset=+1]
2748+
2749+
include::0000-00-note.adoc[leveloffset=+1]

src/main/java/com/diguage/algo/leetcode/_0503_NextGreaterElementII.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ public int[] nextGreaterElements(int[] nums) {
1717
}
1818
int[] result = new int[nums.length];
1919
Deque<Integer> stack = new LinkedList<>();
20+
// 只需要将数组“拼接”,遍历两遍数组,就可以解决所有元素后继更大元素的问题
21+
// 从后向前遍历,再加上单调递增栈,就是时间复杂度为 O(n) 的解决方案
2022
for (int i = 2 * nums.length - 1; i >= 0; i--) {
23+
// 取余即可获取当前需要处理的元素
2124
int index = i % nums.length;
25+
// 在单调栈不为空的情况下,将栈中小于等于当前元素的值都弹出
2226
while (!stack.isEmpty() && stack.peek() <= nums[index]) {
2327
stack.pop();
2428
}
29+
// 剩下元素既是比当前元素大的后继元素。为空则是没有更大元素
30+
// 这里还有一个隐含变量:
31+
// 由于栈是从后向前添加,则栈顶元素距离当前元素更近。
32+
// 如果栈不为空,则栈顶元素就是符合条件的元素。
2533
result[index] = stack.isEmpty() ? -1 : stack.peek();
2634
stack.push(nums[index]);
2735
}

0 commit comments

Comments
 (0)