Skip to content

Commit 1b5ed6a

Browse files
committed
update 230 java, cheatsheet, progress
1 parent 5450914 commit 1b5ed6a

File tree

6 files changed

+192
-25
lines changed

6 files changed

+192
-25
lines changed

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20240515: 104
1+
20240515: 104,230
22
20240514: 105,106
33
20240513: 242,235
44
20240512: 371

data/to_review.txt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1+
2024-07-09 -> ['104,230']
12
2024-07-08 -> ['105,106']
23
2024-07-07 -> ['242,235']
34
2024-07-06 -> ['371']
45
2024-07-05 -> ['121,252']
56
2024-07-04 -> ['125']
7+
2024-06-18 -> ['104,230']
68
2024-06-17 -> ['105,106']
79
2024-06-16 -> ['242,235']
810
2024-06-15 -> ['371']
911
2024-06-14 -> ['121,252']
1012
2024-06-13 -> ['125']
13+
2024-06-05 -> ['104,230']
1114
2024-06-04 -> ['105,106']
1215
2024-06-03 -> ['242,235']
1316
2024-06-02 -> ['371']
1417
2024-06-01 -> ['121,252']
1518
2024-05-31 -> ['125']
19+
2024-05-28 -> ['104,230']
1620
2024-05-27 -> ['105,106']
1721
2024-05-26 -> ['242,235']
1822
2024-05-25 -> ['371']
1923
2024-05-24 -> ['121,252']
20-
2024-05-23 -> ['125']
24+
2024-05-23 -> ['104,230', '125']
2125
2024-05-22 -> ['105,106']
2226
2024-05-21 -> ['242,235']
23-
2024-05-20 -> ['371']
27+
2024-05-20 -> ['104,230', '371']
2428
2024-05-19 -> ['105,106', '121,252']
25-
2024-05-18 -> ['242,235', '125']
26-
2024-05-17 -> ['105,106', '371']
27-
2024-05-16 -> ['105,106', '242,235', '121,252']
29+
2024-05-18 -> ['104,230', '242,235', '125']
30+
2024-05-17 -> ['104,230', '105,106', '371']
31+
2024-05-16 -> ['104,230', '105,106', '242,235', '121,252']
2832
2024-05-15 -> ['105,106', '242,235', '371', '125']
2933
2024-05-14 -> ['242,235', '371', '121,252']
3034
2024-05-13 -> ['371', '121,252', '125']
@@ -347,7 +351,7 @@
347351
2022-10-11 -> ['489']
348352
2022-10-10 -> ['489']
349353
2022-10-09 -> ['052', '240']
350-
2022-10-08 -> ['104', '912', '095']
354+
2022-10-08 -> ['912', '095', '104']
351355
2022-10-07 -> ['700,070']
352356
2022-10-06 -> ['052']
353357
2022-10-04 -> ['052']
@@ -363,7 +367,7 @@
363367
2022-09-20 -> ['240', '912']
364368
2022-09-19 -> ['240', '912']
365369
2022-09-18 -> ['912', '779']
366-
2022-09-17 -> ['104', '095']
370+
2022-09-17 -> ['095', '104']
367371
2022-09-16 -> ['700,070']
368372
2022-09-12 -> ['095']
369373
2022-09-10 -> ['779']

doc/cheatsheet/java_trick.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,36 @@ public int[][] DIRECTIONS = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
536536
```java
537537
```
538538

539+
### 1-19) PQ
540+
```java
541+
542+
// Small PQ (default min-heap)
543+
PriorityQueue<Integer> smallPQ = new PriorityQueue<>();
544+
545+
// Big PQ (max-heap)
546+
PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());
547+
548+
// Add elements to PQs
549+
smallPQ.add(5);
550+
smallPQ.add(10);
551+
smallPQ.add(1);
552+
553+
bigPQ.add(5);
554+
bigPQ.add(10);
555+
bigPQ.add(1);
556+
557+
// Print elements from PQs
558+
System.out.println("Small PQ (min-heap):");
559+
while (!smallPQ.isEmpty()) {
560+
System.out.println(smallPQ.poll());
561+
}
562+
563+
System.out.println("Big PQ (max-heap):");
564+
while (!bigPQ.isEmpty()) {
565+
System.out.println(bigPQ.poll());
566+
}
567+
```
568+
539569
# 2) Other tricks
540570

541571
### 2-1) Init var, modify it in another method, and use it

doc/cheatsheet/priority_queue.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,36 @@
2121

2222
### 1-1) Basic OP
2323

24+
#### 1-1-1) Init a small, big PQ in java
25+
26+
```java
27+
// java
28+
29+
// Small PQ (default min-heap)
30+
PriorityQueue<Integer> smallPQ = new PriorityQueue<>();
31+
32+
// Big PQ (max-heap)
33+
PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());
34+
35+
// Add elements to PQs
36+
smallPQ.add(5);
37+
smallPQ.add(10);
38+
smallPQ.add(1);
39+
40+
bigPQ.add(5);
41+
bigPQ.add(10);
42+
bigPQ.add(1);
43+
44+
// Print elements from PQs
45+
System.out.println("Small PQ (min-heap):");
46+
while (!smallPQ.isEmpty()) {
47+
System.out.println(smallPQ.poll());
48+
}
49+
50+
System.out.println("Big PQ (max-heap):");
51+
while (!bigPQ.isEmpty()) {
52+
System.out.println(bigPQ.poll());
53+
}
54+
```
55+
2456
## 2) LC Example

leetcode_java/src/main/java/LeetCodeJava/BinarySearchTree/KthSmallestElementInABST.java

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
import LeetCodeJava.DataStructure.TreeNode;
66

7-
import java.util.ArrayList;
8-
import java.util.Arrays;
9-
import java.util.LinkedList;
10-
import java.util.List;
7+
import java.util.*;
118
import java.util.stream.Collectors;
129
import java.util.stream.Stream;
1310

@@ -30,9 +27,56 @@
3027
public class KthSmallestElementInABST {
3128

3229
// V0
30+
// IDEA : BFS + PQ
31+
public int kthSmallest(TreeNode root, int k) {
32+
33+
if (root.left == null && root.right == null){
34+
return root.val;
35+
}
36+
37+
/** NOTE !!!
38+
*
39+
* we use PQ (priority queue) for getting k-th small element
40+
*
41+
* In java, default PQ is small PQ, if we need big PQ, can use update comparator
42+
*
43+
*
44+
* // Small PQ (default min-heap)
45+
* PriorityQueue<Integer> smallPQ = new PriorityQueue<>();
46+
*
47+
* // Big PQ (max-heap)
48+
* PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());
49+
*
50+
*/
51+
PriorityQueue<Integer> pq = new PriorityQueue();
52+
Queue<TreeNode> queue = new LinkedList<>();
53+
queue.add(root);
54+
55+
// bfs
56+
while(!queue.isEmpty()){
57+
TreeNode node = queue.poll();
58+
pq.add(node.val);
59+
if (node.left != null){
60+
queue.add(node.left);
61+
}
62+
if (node.right != null){
63+
queue.add(node.right);
64+
}
65+
}
66+
67+
// 1-bases k-th small element
68+
while (k > 1){
69+
pq.poll();
70+
k -= 1;
71+
}
72+
73+
return pq.poll();
74+
}
75+
76+
// V0'
3377
// IDEA : DFS
3478
List<Integer> cache = new ArrayList();
35-
public int kthSmallest(TreeNode root, int k) {
79+
public int kthSmallest_0(TreeNode root, int k) {
3680

3781
if (root.left == null && root.right == null){
3882
return k;
@@ -66,9 +110,6 @@ private void dfs(TreeNode root){
66110
}
67111
}
68112

69-
// V0'
70-
// IDEA : BFS
71-
72113
// V1
73114
// IDEA : Recursive Inorder Traversal + STACK
74115
// https://leetcode.com/problems/kth-smallest-element-in-a-bst/editorial/

leetcode_java/src/main/java/dev/workspace3.java

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,45 @@ public static void main(String[] args) {
2323
// System.out.println("[" + threadInfo.getThreadId() + "] " + threadInfo.getThreadName());
2424
// }
2525

26-
int n1 = 1;
27-
int n2 = 2;
28-
int n3 = 0;
26+
// int n1 = 1;
27+
// int n2 = 2;
28+
// int n3 = 0;
29+
//
30+
// String binary1 = Integer.toBinaryString(n1);
31+
// String binary2 = Integer.toBinaryString(2);
32+
// String binary3 = Integer.toBinaryString(3);
33+
//
34+
// System.out.println(n1 + " in binary is: " + binary1);
35+
// System.out.println(n2 + " in binary is: " + binary2);
36+
// System.out.println(n3 + " in binary is: " + binary3);
37+
38+
39+
40+
// Small PQ (default min-heap)
41+
PriorityQueue<Integer> smallPQ = new PriorityQueue<>();
42+
43+
// Big PQ (max-heap)
44+
PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());
2945

30-
String binary1 = Integer.toBinaryString(n1);
31-
String binary2 = Integer.toBinaryString(2);
32-
String binary3 = Integer.toBinaryString(3);
46+
// Add elements to PQs
47+
smallPQ.add(5);
48+
smallPQ.add(10);
49+
smallPQ.add(1);
3350

34-
System.out.println(n1 + " in binary is: " + binary1);
35-
System.out.println(n2 + " in binary is: " + binary2);
36-
System.out.println(n3 + " in binary is: " + binary3);
51+
bigPQ.add(5);
52+
bigPQ.add(10);
53+
bigPQ.add(1);
54+
55+
// Print elements from PQs
56+
System.out.println("Small PQ (min-heap):");
57+
while (!smallPQ.isEmpty()) {
58+
System.out.println(smallPQ.poll());
59+
}
60+
61+
System.out.println("Big PQ (max-heap):");
62+
while (!bigPQ.isEmpty()) {
63+
System.out.println(bigPQ.poll());
64+
}
3765

3866
}
3967

@@ -349,4 +377,36 @@ public int maxDepth(TreeNode root) {
349377
return Math.max(leftDepth, rightDepth);
350378
}
351379

380+
381+
// LC 230
382+
// bfs
383+
public int kthSmallest(TreeNode root, int k) {
384+
385+
if (root.left == null && root.right == null){
386+
return root.val;
387+
}
388+
389+
PriorityQueue<Integer> pq = new PriorityQueue();
390+
Queue<TreeNode> queue = new LinkedList<>();
391+
queue.add(root);
392+
393+
while(!queue.isEmpty()){
394+
TreeNode node = queue.poll();
395+
pq.add(node.val);
396+
if (node.left != null){
397+
queue.add(node.left);
398+
}
399+
if (node.right != null){
400+
queue.add(node.right);
401+
}
402+
}
403+
404+
while (k > 1){
405+
pq.poll();
406+
k -= 1;
407+
}
408+
409+
return pq.poll();
410+
}
411+
352412
}

0 commit comments

Comments
 (0)