Skip to content

Commit 3c85d1a

Browse files
Update README.md
1 parent 8bc0418 commit 3c85d1a

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

_notes_others/leetcode_dsa_patterns/README.md

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
## Data Structures & Algorithms Patterns
1+
# Data Structures & Algorithms Patterns
22

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).
44

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.
8+
9+
![flowchart](https://github.com/Suryakant-Bharti/Important-Java-Concepts/assets/2780145/66a204d9-5e7d-484a-b04d-71ac2f1c2c86)
10+
11+
### 1) Array - Two pointers: one input, opposite ends
612

713
```java
814
public int fn(int[] arr) {
@@ -24,7 +30,7 @@ public int fn(int[] arr) {
2430
```
2531

2632

27-
## 2) Array - Two pointers: two inputs, exhaust both
33+
### 2) Array - Two pointers: two inputs, exhaust both
2834

2935
```java
3036
public int fn(int[] arr1, int[] arr2) {
@@ -54,7 +60,7 @@ public int fn(int[] arr1, int[] arr2) {
5460
```
5561

5662

57-
## 3) Array - Sliding window
63+
### 3) Array - Sliding window
5864

5965
```java
6066
public int fn(int[] arr) {
@@ -76,7 +82,7 @@ public int fn(int[] arr) {
7682
```
7783

7884

79-
## 4) Array - Build a prefix sum
85+
### 4) Array - Build a prefix sum
8086

8187
```java
8288
public int[] fn(int[] arr) {
@@ -92,7 +98,7 @@ public int[] fn(int[] arr) {
9298
```
9399

94100

95-
## 5) Efficient string building
101+
### 5) Efficient string building
96102

97103
```java
98104
public String fn(char[] arr) {
@@ -106,7 +112,7 @@ public String fn(char[] arr) {
106112
```
107113

108114

109-
## 6) Linked list: fast and slow pointer
115+
### 6) Linked list: fast and slow pointer
110116

111117
```java
112118
public int fn(ListNode head) {
@@ -125,7 +131,7 @@ public int fn(ListNode head) {
125131
```
126132

127133

128-
## 7) Reversing a linked list
134+
### 7) Reversing a linked list
129135

130136
```java
131137
public ListNode fn(ListNode head) {
@@ -143,7 +149,7 @@ public ListNode fn(ListNode head) {
143149
```
144150

145151

146-
## 8) Find number of subarrays that fit an exact criteria
152+
### 8) Find number of subarrays that fit an exact criteria
147153

148154
```java
149155
public int fn(int[] arr, int k) {
@@ -162,7 +168,7 @@ public int fn(int[] arr, int k) {
162168
```
163169

164170

165-
## 9) Monotonic increasing stack
171+
### 9) Monotonic increasing stack
166172

167173
The same logic can be applied to maintain a monotonic queue.
168174

@@ -186,7 +192,7 @@ public int fn(int[] arr) {
186192
```
187193

188194

189-
## 10) Binary tree: DFS (recursive)
195+
### 10) Binary tree: DFS (recursive)
190196

191197
```java
192198
public int dfs(TreeNode root) {
@@ -203,7 +209,7 @@ public int dfs(TreeNode root) {
203209
```
204210

205211

206-
## 11) Binary tree: DFS (recursive)
212+
### 11) Binary tree: DFS (recursive)
207213

208214
```java
209215
public int dfs(TreeNode root) {
@@ -227,7 +233,7 @@ public int dfs(TreeNode root) {
227233
```
228234

229235

230-
## 12) Binary tree: BFS
236+
### 12) Binary tree: BFS
231237

232238
```java
233239
public int fn(TreeNode root) {
@@ -256,7 +262,7 @@ public int fn(TreeNode root) {
256262
```
257263

258264

259-
## 13) Graph: DFS (recursive)
265+
### 13) Graph: DFS (recursive)
260266

261267
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.
262268

@@ -283,7 +289,7 @@ public int dfs(int node, int[][] graph) {
283289
```
284290

285291

286-
## 14) Graph: DFS (iterative)
292+
### 14) Graph: DFS (iterative)
287293

288294
```java
289295
public int fn(int[][] graph) {
@@ -309,7 +315,7 @@ public int fn(int[][] graph) {
309315
```
310316

311317

312-
## 15) Graph: BFS
318+
### 15) Graph: BFS
313319

314320
```java
315321
public int fn(int[][] graph) {
@@ -335,9 +341,7 @@ public int fn(int[][] graph) {
335341
```
336342

337343

338-
## 16) Find top k elements with heap
339-
340-
344+
### 16) Find top k elements with heap
341345

342346
```java
343347
public int[] fn(int[] arr, int k) {
@@ -359,7 +363,7 @@ public int[] fn(int[] arr, int k) {
359363
```
360364

361365

362-
## 17) Binary search
366+
### 17) Binary search
363367

364368
```java
365369
public int fn(int[] arr, int target) {
@@ -384,7 +388,7 @@ public int fn(int[] arr, int target) {
384388
```
385389

386390

387-
## 18) Binary search: duplicate elements, left-most insertion point
391+
### 18) Binary search: duplicate elements, left-most insertion point
388392

389393
```java
390394
public int fn(int[] arr, int target) {
@@ -404,7 +408,7 @@ public int fn(int[] arr, int target) {
404408
```
405409

406410

407-
## 19) Binary search: duplicate elements, right-most insertion point
411+
### 19) Binary search: duplicate elements, right-most insertion point
408412

409413
```java
410414
public int fn(int[] arr, int target) {
@@ -424,7 +428,7 @@ public int fn(int[] arr, int target) {
424428
```
425429

426430

427-
## 20) Binary search: for greedy problems - looking for minimum
431+
### 20) Binary search: for greedy problems - looking for minimum
428432

429433
```java
430434
public int fn(int[] arr) {
@@ -449,7 +453,7 @@ public boolean check(int x) {
449453
```
450454

451455

452-
## 21) Binary search: for greedy problems - looking for maximum
456+
### 21) Binary search: for greedy problems - looking for maximum
453457

454458
```java
455459
public int fn(int[] arr) {
@@ -474,7 +478,7 @@ public boolean check(int x) {
474478
```
475479

476480

477-
## 22) Backtracking problems
481+
### 22) Backtracking problems
478482

479483
```java
480484
public int backtrack(STATE curr, OTHER_ARGUMENTS...) {
@@ -493,7 +497,7 @@ public int backtrack(STATE curr, OTHER_ARGUMENTS...) {
493497
```
494498

495499

496-
## 23) Dynamic programming: top-down memoization
500+
### 23) Dynamic programming: top-down memoization
497501

498502
```java
499503
Map<STATE, Integer> memo = new HashMap<>();
@@ -518,7 +522,7 @@ public int dp(STATE, int[] arr) {
518522
```
519523

520524

521-
## 24) Build a trie
525+
### 24) Build a trie
522526

523527
```java
524528
// note: using a class is only necessary if you want to store data at each node.
@@ -553,7 +557,7 @@ public TrieNode buildTrie(String[] words) {
553557
```
554558

555559

556-
## 25) Dijkstra's algorithm
560+
### 25) Dijkstra's algorithm
557561

558562
```java
559563
int[] distances = new int[n];

0 commit comments

Comments
 (0)