You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _notes_others/leetcode_dsa_patterns/README.md
+33-29Lines changed: 33 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,14 @@
1
-
##Data Structures & Algorithms Patterns
1
+
# Data Structures & Algorithms Patterns
2
2
3
-
Code templetes for [Data Structures and Algorithms Crash Course](https://leetcode.com/explore/interview/card/leetcodes-interview-crash-course-data-structures-and-algorithms/) from LeetCode & some mine.
3
+
Code templetes for [Data Structures and Algorithms Crash Course](https://leetcode.com/explore/interview/card/leetcodes-interview-crash-course-data-structures-and-algorithms/) from LeetCode (with some edits from me).
4
4
5
-
## 1) Array - Two pointers: one input, opposite ends
5
+
### Helper Flowchart
6
+
7
+
NOTE: This is a helper flowchart, it covers majority of problems. But, it is not possible to cover all possible problems.
### 1) Array - Two pointers: one input, opposite ends
6
12
7
13
```java
8
14
publicint fn(int[] arr) {
@@ -24,7 +30,7 @@ public int fn(int[] arr) {
24
30
```
25
31
26
32
27
-
## 2) Array - Two pointers: two inputs, exhaust both
33
+
###2) Array - Two pointers: two inputs, exhaust both
28
34
29
35
```java
30
36
publicint fn(int[] arr1, int[] arr2) {
@@ -54,7 +60,7 @@ public int fn(int[] arr1, int[] arr2) {
54
60
```
55
61
56
62
57
-
## 3) Array - Sliding window
63
+
###3) Array - Sliding window
58
64
59
65
```java
60
66
publicint fn(int[] arr) {
@@ -76,7 +82,7 @@ public int fn(int[] arr) {
76
82
```
77
83
78
84
79
-
## 4) Array - Build a prefix sum
85
+
###4) Array - Build a prefix sum
80
86
81
87
```java
82
88
publicint[] fn(int[] arr) {
@@ -92,7 +98,7 @@ public int[] fn(int[] arr) {
92
98
```
93
99
94
100
95
-
## 5) Efficient string building
101
+
###5) Efficient string building
96
102
97
103
```java
98
104
publicString fn(char[] arr) {
@@ -106,7 +112,7 @@ public String fn(char[] arr) {
106
112
```
107
113
108
114
109
-
## 6) Linked list: fast and slow pointer
115
+
###6) Linked list: fast and slow pointer
110
116
111
117
```java
112
118
publicint fn(ListNode head) {
@@ -125,7 +131,7 @@ public int fn(ListNode head) {
125
131
```
126
132
127
133
128
-
## 7) Reversing a linked list
134
+
###7) Reversing a linked list
129
135
130
136
```java
131
137
publicListNode fn(ListNode head) {
@@ -143,7 +149,7 @@ public ListNode fn(ListNode head) {
143
149
```
144
150
145
151
146
-
## 8) Find number of subarrays that fit an exact criteria
152
+
###8) Find number of subarrays that fit an exact criteria
147
153
148
154
```java
149
155
publicint fn(int[] arr, int k) {
@@ -162,7 +168,7 @@ public int fn(int[] arr, int k) {
162
168
```
163
169
164
170
165
-
## 9) Monotonic increasing stack
171
+
###9) Monotonic increasing stack
166
172
167
173
The same logic can be applied to maintain a monotonic queue.
168
174
@@ -186,7 +192,7 @@ public int fn(int[] arr) {
186
192
```
187
193
188
194
189
-
## 10) Binary tree: DFS (recursive)
195
+
###10) Binary tree: DFS (recursive)
190
196
191
197
```java
192
198
publicint dfs(TreeNode root) {
@@ -203,7 +209,7 @@ public int dfs(TreeNode root) {
203
209
```
204
210
205
211
206
-
## 11) Binary tree: DFS (recursive)
212
+
###11) Binary tree: DFS (recursive)
207
213
208
214
```java
209
215
publicint dfs(TreeNode root) {
@@ -227,7 +233,7 @@ public int dfs(TreeNode root) {
227
233
```
228
234
229
235
230
-
## 12) Binary tree: BFS
236
+
###12) Binary tree: BFS
231
237
232
238
```java
233
239
publicint fn(TreeNode root) {
@@ -256,7 +262,7 @@ public int fn(TreeNode root) {
256
262
```
257
263
258
264
259
-
## 13) Graph: DFS (recursive)
265
+
###13) Graph: DFS (recursive)
260
266
261
267
For the graph templates, assume the nodes are numbered from 0 to n - 1 and the graph is given as an adjacency list. Depending on the problem, you may need to convert the input into an equivalent adjacency list before using the templates.
262
268
@@ -283,7 +289,7 @@ public int dfs(int node, int[][] graph) {
283
289
```
284
290
285
291
286
-
## 14) Graph: DFS (iterative)
292
+
###14) Graph: DFS (iterative)
287
293
288
294
```java
289
295
publicint fn(int[][] graph) {
@@ -309,7 +315,7 @@ public int fn(int[][] graph) {
309
315
```
310
316
311
317
312
-
## 15) Graph: BFS
318
+
###15) Graph: BFS
313
319
314
320
```java
315
321
publicint fn(int[][] graph) {
@@ -335,9 +341,7 @@ public int fn(int[][] graph) {
335
341
```
336
342
337
343
338
-
## 16) Find top k elements with heap
339
-
340
-
344
+
### 16) Find top k elements with heap
341
345
342
346
```java
343
347
publicint[] fn(int[] arr, int k) {
@@ -359,7 +363,7 @@ public int[] fn(int[] arr, int k) {
359
363
```
360
364
361
365
362
-
## 17) Binary search
366
+
###17) Binary search
363
367
364
368
```java
365
369
publicint fn(int[] arr, int target) {
@@ -384,7 +388,7 @@ public int fn(int[] arr, int target) {
384
388
```
385
389
386
390
387
-
## 18) Binary search: duplicate elements, left-most insertion point
391
+
###18) Binary search: duplicate elements, left-most insertion point
388
392
389
393
```java
390
394
publicint fn(int[] arr, int target) {
@@ -404,7 +408,7 @@ public int fn(int[] arr, int target) {
404
408
```
405
409
406
410
407
-
## 19) Binary search: duplicate elements, right-most insertion point
411
+
###19) Binary search: duplicate elements, right-most insertion point
408
412
409
413
```java
410
414
publicint fn(int[] arr, int target) {
@@ -424,7 +428,7 @@ public int fn(int[] arr, int target) {
424
428
```
425
429
426
430
427
-
## 20) Binary search: for greedy problems - looking for minimum
431
+
###20) Binary search: for greedy problems - looking for minimum
428
432
429
433
```java
430
434
publicint fn(int[] arr) {
@@ -449,7 +453,7 @@ public boolean check(int x) {
449
453
```
450
454
451
455
452
-
## 21) Binary search: for greedy problems - looking for maximum
456
+
###21) Binary search: for greedy problems - looking for maximum
0 commit comments