Skip to content

Commit b303816

Browse files
Update README.md
1 parent e9033b9 commit b303816

File tree

1 file changed

+95
-93
lines changed

1 file changed

+95
-93
lines changed

_notes_others/leetcode_dsa_patterns/README.md

Lines changed: 95 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -155,44 +155,7 @@ public String fn(char[] arr) {
155155
```
156156

157157

158-
### 6) Linked list: fast and slow pointer
159-
160-
```java
161-
public int fn(ListNode head) {
162-
ListNode slow = head;
163-
ListNode fast = head;
164-
int ans = 0;
165-
166-
while (fast != null && fast.next != null) {
167-
// do logic
168-
slow = slow.next;
169-
fast = fast.next.next;
170-
}
171-
172-
return ans;
173-
}
174-
```
175-
176-
177-
### 7) Reversing a linked list
178-
179-
```java
180-
public ListNode fn(ListNode head) {
181-
ListNode curr = head;
182-
ListNode prev = null;
183-
while (curr != null) {
184-
ListNode nextNode = curr.next;
185-
curr.next = prev;
186-
prev = curr;
187-
curr = nextNode;
188-
}
189-
190-
return prev;
191-
}
192-
```
193-
194-
195-
### 8) Find number of subarrays that fit an exact criteria
158+
### 6) Find number of subarrays that fit an exact criteria
196159

197160
```java
198161
public int fn(int[] arr, int k) {
@@ -211,7 +174,32 @@ public int fn(int[] arr, int k) {
211174
```
212175

213176

214-
### 9) Monotonic increasing stack
177+
### 7) Binary search
178+
179+
```java
180+
public int fn(int[] arr, int target) {
181+
int left = 0;
182+
int right = arr.length - 1;
183+
while (left <= right) {
184+
int mid = left + (right - left) / 2;
185+
if (arr[mid] == target) {
186+
// do something
187+
return mid;
188+
}
189+
if (arr[mid] > target) {
190+
right = mid - 1;
191+
} else {
192+
left = mid + 1;
193+
}
194+
}
195+
196+
// left is the insertion point
197+
return left;
198+
}
199+
```
200+
201+
202+
### 8) Monotonic increasing stack
215203

216204
The same logic can be applied to maintain a monotonic queue.
217205

@@ -235,7 +223,66 @@ public int fn(int[] arr) {
235223
```
236224

237225

238-
### 10) Binary tree: DFS (recursive)
226+
### 9) Find top k elements with heap
227+
228+
```java
229+
public int[] fn(int[] arr, int k) {
230+
PriorityQueue<Integer> heap = new PriorityQueue<>(CRITERIA);
231+
for (int num: arr) {
232+
heap.add(num);
233+
if (heap.size() > k) {
234+
heap.remove();
235+
}
236+
}
237+
238+
int[] ans = new int[k];
239+
for (int i = 0; i < k; i++) {
240+
ans[i] = heap.remove();
241+
}
242+
243+
return ans;
244+
}
245+
```
246+
247+
248+
### 10) Linked list: fast and slow pointer
249+
250+
```java
251+
public int fn(ListNode head) {
252+
ListNode slow = head;
253+
ListNode fast = head;
254+
int ans = 0;
255+
256+
while (fast != null && fast.next != null) {
257+
// do logic
258+
slow = slow.next;
259+
fast = fast.next.next;
260+
}
261+
262+
return ans;
263+
}
264+
```
265+
266+
267+
### 11) Reversing a linked list
268+
269+
```java
270+
public ListNode fn(ListNode head) {
271+
ListNode curr = head;
272+
ListNode prev = null;
273+
while (curr != null) {
274+
ListNode nextNode = curr.next;
275+
curr.next = prev;
276+
prev = curr;
277+
curr = nextNode;
278+
}
279+
280+
return prev;
281+
}
282+
```
283+
284+
285+
### 12) Binary tree: DFS (recursive)
239286

240287
```java
241288
public int dfs(TreeNode root) {
@@ -252,7 +299,7 @@ public int dfs(TreeNode root) {
252299
```
253300

254301

255-
### 11) Binary tree: DFS (recursive)
302+
### 13) Binary tree: DFS (iterative)
256303

257304
```java
258305
public int dfs(TreeNode root) {
@@ -276,7 +323,7 @@ public int dfs(TreeNode root) {
276323
```
277324

278325

279-
### 12) Binary tree: BFS
326+
### 14) Binary tree: BFS (iterative)
280327

281328
```java
282329
public int fn(TreeNode root) {
@@ -305,9 +352,11 @@ public int fn(TreeNode root) {
305352
```
306353

307354

308-
### 13) Graph: DFS (recursive)
355+
### 15) Graph: DFS (recursive)
356+
357+
For the graph templates, assume the nodes are numbered from 0 to n - 1 and the graph is given as an adjacency list.
309358

310-
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.
359+
Depending on the problem, you may need to convert the input into an equivalent adjacency list before using the templates.
311360

312361
```java
313362
Set<Integer> seen = new HashSet<>();
@@ -332,7 +381,7 @@ public int dfs(int node, int[][] graph) {
332381
```
333382

334383

335-
### 14) Graph: DFS (iterative)
384+
### 16) Graph: DFS (iterative)
336385

337386
```java
338387
public int fn(int[][] graph) {
@@ -358,7 +407,7 @@ public int fn(int[][] graph) {
358407
```
359408

360409

361-
### 15) Graph: BFS
410+
### 17) Graph: BFS (iterative)
362411

363412
```java
364413
public int fn(int[][] graph) {
@@ -384,53 +433,6 @@ public int fn(int[][] graph) {
384433
```
385434

386435

387-
### 16) Find top k elements with heap
388-
389-
```java
390-
public int[] fn(int[] arr, int k) {
391-
PriorityQueue<Integer> heap = new PriorityQueue<>(CRITERIA);
392-
for (int num: arr) {
393-
heap.add(num);
394-
if (heap.size() > k) {
395-
heap.remove();
396-
}
397-
}
398-
399-
int[] ans = new int[k];
400-
for (int i = 0; i < k; i++) {
401-
ans[i] = heap.remove();
402-
}
403-
404-
return ans;
405-
}
406-
```
407-
408-
409-
### 17) Binary search
410-
411-
```java
412-
public int fn(int[] arr, int target) {
413-
int left = 0;
414-
int right = arr.length - 1;
415-
while (left <= right) {
416-
int mid = left + (right - left) / 2;
417-
if (arr[mid] == target) {
418-
// do something
419-
return mid;
420-
}
421-
if (arr[mid] > target) {
422-
right = mid - 1;
423-
} else {
424-
left = mid + 1;
425-
}
426-
}
427-
428-
// left is the insertion point
429-
return left;
430-
}
431-
```
432-
433-
434436
### 18) Binary search: duplicate elements, left-most insertion point
435437

436438
```java

0 commit comments

Comments
 (0)