diff --git a/data/progress.txt b/data/progress.txt index 10fbbe26..88f82f14 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240515: 104 +20240515: 104,230 20240514: 105,106 20240513: 242,235 20240512: 371 diff --git a/data/to_review.txt b/data/to_review.txt index f66e895f..1835d7ab 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] @@ -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'] @@ -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'] diff --git a/doc/cheatsheet/java_trick.md b/doc/cheatsheet/java_trick.md index b5aed74e..744ed9fb 100644 --- a/doc/cheatsheet/java_trick.md +++ b/doc/cheatsheet/java_trick.md @@ -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 smallPQ = new PriorityQueue<>(); + +// Big PQ (max-heap) +PriorityQueue 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 diff --git a/doc/cheatsheet/priority_queue.md b/doc/cheatsheet/priority_queue.md index 8e11897a..8c8004a3 100644 --- a/doc/cheatsheet/priority_queue.md +++ b/doc/cheatsheet/priority_queue.md @@ -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 smallPQ = new PriorityQueue<>(); + +// Big PQ (max-heap) +PriorityQueue 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 diff --git a/leetcode_java/src/main/java/LeetCodeJava/BinarySearchTree/KthSmallestElementInABST.java b/leetcode_java/src/main/java/LeetCodeJava/BinarySearchTree/KthSmallestElementInABST.java index 1cc63bdd..e465cf5c 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/BinarySearchTree/KthSmallestElementInABST.java +++ b/leetcode_java/src/main/java/LeetCodeJava/BinarySearchTree/KthSmallestElementInABST.java @@ -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; @@ -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 smallPQ = new PriorityQueue<>(); + * + * // Big PQ (max-heap) + * PriorityQueue bigPQ = new PriorityQueue<>(Comparator.reverseOrder()); + * + */ + PriorityQueue pq = new PriorityQueue(); + Queue 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 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; @@ -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/ diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index f2013c3d..ba93dc84 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -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 smallPQ = new PriorityQueue<>(); + + // Big PQ (max-heap) + PriorityQueue 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()); + } } @@ -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 pq = new PriorityQueue(); + Queue 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(); + } + }