Skip to content

Commit

Permalink
update 230 java, cheatsheet, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 15, 2024
1 parent 5450914 commit 1b5ed6a
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 25 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240515: 104
20240515: 104,230
20240514: 105,106
20240513: 242,235
20240512: 371
Expand Down
18 changes: 11 additions & 7 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
2024-07-09 -> ['104,230']
2024-07-08 -> ['105,106']
2024-07-07 -> ['242,235']
2024-07-06 -> ['371']
2024-07-05 -> ['121,252']
2024-07-04 -> ['125']
2024-06-18 -> ['104,230']
2024-06-17 -> ['105,106']
2024-06-16 -> ['242,235']
2024-06-15 -> ['371']
2024-06-14 -> ['121,252']
2024-06-13 -> ['125']
2024-06-05 -> ['104,230']
2024-06-04 -> ['105,106']
2024-06-03 -> ['242,235']
2024-06-02 -> ['371']
2024-06-01 -> ['121,252']
2024-05-31 -> ['125']
2024-05-28 -> ['104,230']
2024-05-27 -> ['105,106']
2024-05-26 -> ['242,235']
2024-05-25 -> ['371']
2024-05-24 -> ['121,252']
2024-05-23 -> ['125']
2024-05-23 -> ['104,230', '125']
2024-05-22 -> ['105,106']
2024-05-21 -> ['242,235']
2024-05-20 -> ['371']
2024-05-20 -> ['104,230', '371']
2024-05-19 -> ['105,106', '121,252']
2024-05-18 -> ['242,235', '125']
2024-05-17 -> ['105,106', '371']
2024-05-16 -> ['105,106', '242,235', '121,252']
2024-05-18 -> ['104,230', '242,235', '125']
2024-05-17 -> ['104,230', '105,106', '371']
2024-05-16 -> ['104,230', '105,106', '242,235', '121,252']
2024-05-15 -> ['105,106', '242,235', '371', '125']
2024-05-14 -> ['242,235', '371', '121,252']
2024-05-13 -> ['371', '121,252', '125']
Expand Down Expand Up @@ -347,7 +351,7 @@
2022-10-11 -> ['489']
2022-10-10 -> ['489']
2022-10-09 -> ['052', '240']
2022-10-08 -> ['104', '912', '095']
2022-10-08 -> ['912', '095', '104']
2022-10-07 -> ['700,070']
2022-10-06 -> ['052']
2022-10-04 -> ['052']
Expand All @@ -363,7 +367,7 @@
2022-09-20 -> ['240', '912']
2022-09-19 -> ['240', '912']
2022-09-18 -> ['912', '779']
2022-09-17 -> ['104', '095']
2022-09-17 -> ['095', '104']
2022-09-16 -> ['700,070']
2022-09-12 -> ['095']
2022-09-10 -> ['779']
Expand Down
30 changes: 30 additions & 0 deletions doc/cheatsheet/java_trick.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,36 @@ public int[][] DIRECTIONS = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
```java
```

### 1-19) PQ
```java

// Small PQ (default min-heap)
PriorityQueue<Integer> smallPQ = new PriorityQueue<>();

// Big PQ (max-heap)
PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());

// Add elements to PQs
smallPQ.add(5);
smallPQ.add(10);
smallPQ.add(1);

bigPQ.add(5);
bigPQ.add(10);
bigPQ.add(1);

// Print elements from PQs
System.out.println("Small PQ (min-heap):");
while (!smallPQ.isEmpty()) {
System.out.println(smallPQ.poll());
}

System.out.println("Big PQ (max-heap):");
while (!bigPQ.isEmpty()) {
System.out.println(bigPQ.poll());
}
```

# 2) Other tricks

### 2-1) Init var, modify it in another method, and use it
Expand Down
32 changes: 32 additions & 0 deletions doc/cheatsheet/priority_queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,36 @@

### 1-1) Basic OP

#### 1-1-1) Init a small, big PQ in java

```java
// java

// Small PQ (default min-heap)
PriorityQueue<Integer> smallPQ = new PriorityQueue<>();

// Big PQ (max-heap)
PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());

// Add elements to PQs
smallPQ.add(5);
smallPQ.add(10);
smallPQ.add(1);

bigPQ.add(5);
bigPQ.add(10);
bigPQ.add(1);

// Print elements from PQs
System.out.println("Small PQ (min-heap):");
while (!smallPQ.isEmpty()) {
System.out.println(smallPQ.poll());
}

System.out.println("Big PQ (max-heap):");
while (!bigPQ.isEmpty()) {
System.out.println(bigPQ.poll());
}
```

## 2) LC Example
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

import LeetCodeJava.DataStructure.TreeNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -30,9 +27,56 @@
public class KthSmallestElementInABST {

// V0
// IDEA : BFS + PQ
public int kthSmallest(TreeNode root, int k) {

if (root.left == null && root.right == null){
return root.val;
}

/** NOTE !!!
*
* we use PQ (priority queue) for getting k-th small element
*
* In java, default PQ is small PQ, if we need big PQ, can use update comparator
*
*
* // Small PQ (default min-heap)
* PriorityQueue<Integer> smallPQ = new PriorityQueue<>();
*
* // Big PQ (max-heap)
* PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());
*
*/
PriorityQueue<Integer> pq = new PriorityQueue();
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);

// bfs
while(!queue.isEmpty()){
TreeNode node = queue.poll();
pq.add(node.val);
if (node.left != null){
queue.add(node.left);
}
if (node.right != null){
queue.add(node.right);
}
}

// 1-bases k-th small element
while (k > 1){
pq.poll();
k -= 1;
}

return pq.poll();
}

// V0'
// IDEA : DFS
List<Integer> cache = new ArrayList();
public int kthSmallest(TreeNode root, int k) {
public int kthSmallest_0(TreeNode root, int k) {

if (root.left == null && root.right == null){
return k;
Expand Down Expand Up @@ -66,9 +110,6 @@ private void dfs(TreeNode root){
}
}

// V0'
// IDEA : BFS

// V1
// IDEA : Recursive Inorder Traversal + STACK
// https://leetcode.com/problems/kth-smallest-element-in-a-bst/editorial/
Expand Down
78 changes: 69 additions & 9 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,45 @@ public static void main(String[] args) {
// System.out.println("[" + threadInfo.getThreadId() + "] " + threadInfo.getThreadName());
// }

int n1 = 1;
int n2 = 2;
int n3 = 0;
// int n1 = 1;
// int n2 = 2;
// int n3 = 0;
//
// String binary1 = Integer.toBinaryString(n1);
// String binary2 = Integer.toBinaryString(2);
// String binary3 = Integer.toBinaryString(3);
//
// System.out.println(n1 + " in binary is: " + binary1);
// System.out.println(n2 + " in binary is: " + binary2);
// System.out.println(n3 + " in binary is: " + binary3);



// Small PQ (default min-heap)
PriorityQueue<Integer> smallPQ = new PriorityQueue<>();

// Big PQ (max-heap)
PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());

String binary1 = Integer.toBinaryString(n1);
String binary2 = Integer.toBinaryString(2);
String binary3 = Integer.toBinaryString(3);
// Add elements to PQs
smallPQ.add(5);
smallPQ.add(10);
smallPQ.add(1);

System.out.println(n1 + " in binary is: " + binary1);
System.out.println(n2 + " in binary is: " + binary2);
System.out.println(n3 + " in binary is: " + binary3);
bigPQ.add(5);
bigPQ.add(10);
bigPQ.add(1);

// Print elements from PQs
System.out.println("Small PQ (min-heap):");
while (!smallPQ.isEmpty()) {
System.out.println(smallPQ.poll());
}

System.out.println("Big PQ (max-heap):");
while (!bigPQ.isEmpty()) {
System.out.println(bigPQ.poll());
}

}

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


// LC 230
// bfs
public int kthSmallest(TreeNode root, int k) {

if (root.left == null && root.right == null){
return root.val;
}

PriorityQueue<Integer> pq = new PriorityQueue();
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);

while(!queue.isEmpty()){
TreeNode node = queue.poll();
pq.add(node.val);
if (node.left != null){
queue.add(node.left);
}
if (node.right != null){
queue.add(node.right);
}
}

while (k > 1){
pq.poll();
k -= 1;
}

return pq.poll();
}

}

0 comments on commit 1b5ed6a

Please sign in to comment.