diff --git a/data/progress.txt b/data/progress.txt index a7bbb7a9..c108c859 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240518: 213(todo),198(todo),212(todo),211 +20240518: 213(todo),198(todo),212(todo),211,338 20240517: 347,253(todo),91(todo),217 20240516: 226,98,253(todo) 20240515: 104,230,102,100 diff --git a/data/to_review.txt b/data/to_review.txt index 08aa6c8a..e03588ea 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2024-07-12 -> ['213(todo),198(todo),212(todo),211'] +2024-07-12 -> ['213(todo),198(todo),212(todo),211,338'] 2024-07-11 -> ['347,253(todo),91(todo),217'] 2024-07-10 -> ['226,98,253(todo)'] 2024-07-09 -> ['104,230,102,100'] @@ -7,7 +7,7 @@ 2024-07-06 -> ['371'] 2024-07-05 -> ['121,252'] 2024-07-04 -> ['125'] -2024-06-21 -> ['213(todo),198(todo),212(todo),211'] +2024-06-21 -> ['213(todo),198(todo),212(todo),211,338'] 2024-06-20 -> ['347,253(todo),91(todo),217'] 2024-06-19 -> ['226,98,253(todo)'] 2024-06-18 -> ['104,230,102,100'] @@ -16,7 +16,7 @@ 2024-06-15 -> ['371'] 2024-06-14 -> ['121,252'] 2024-06-13 -> ['125'] -2024-06-08 -> ['213(todo),198(todo),212(todo),211'] +2024-06-08 -> ['213(todo),198(todo),212(todo),211,338'] 2024-06-07 -> ['347,253(todo),91(todo),217'] 2024-06-06 -> ['226,98,253(todo)'] 2024-06-05 -> ['104,230,102,100'] @@ -24,19 +24,19 @@ 2024-06-03 -> ['242,235'] 2024-06-02 -> ['371'] 2024-06-01 -> ['121,252'] -2024-05-31 -> ['213(todo),198(todo),212(todo),211', '125'] +2024-05-31 -> ['213(todo),198(todo),212(todo),211,338', '125'] 2024-05-30 -> ['347,253(todo),91(todo),217'] 2024-05-29 -> ['226,98,253(todo)'] 2024-05-28 -> ['104,230,102,100'] 2024-05-27 -> ['105,106'] -2024-05-26 -> ['213(todo),198(todo),212(todo),211', '242,235'] +2024-05-26 -> ['213(todo),198(todo),212(todo),211,338', '242,235'] 2024-05-25 -> ['347,253(todo),91(todo),217', '371'] 2024-05-24 -> ['226,98,253(todo)', '121,252'] -2024-05-23 -> ['213(todo),198(todo),212(todo),211', '104,230,102,100', '125'] +2024-05-23 -> ['213(todo),198(todo),212(todo),211,338', '104,230,102,100', '125'] 2024-05-22 -> ['347,253(todo),91(todo),217', '105,106'] -2024-05-21 -> ['213(todo),198(todo),212(todo),211', '226,98,253(todo)', '242,235'] -2024-05-20 -> ['213(todo),198(todo),212(todo),211', '347,253(todo),91(todo),217', '104,230,102,100', '371'] -2024-05-19 -> ['213(todo),198(todo),212(todo),211', '347,253(todo),91(todo),217', '226,98,253(todo)', '105,106', '121,252'] +2024-05-21 -> ['213(todo),198(todo),212(todo),211,338', '226,98,253(todo)', '242,235'] +2024-05-20 -> ['213(todo),198(todo),212(todo),211,338', '347,253(todo),91(todo),217', '104,230,102,100', '371'] +2024-05-19 -> ['213(todo),198(todo),212(todo),211,338', '347,253(todo),91(todo),217', '226,98,253(todo)', '105,106', '121,252'] 2024-05-18 -> ['347,253(todo),91(todo),217', '226,98,253(todo)', '104,230,102,100', '242,235', '125'] 2024-05-17 -> ['226,98,253(todo)', '104,230,102,100', '105,106', '371'] 2024-05-16 -> ['104,230,102,100', '105,106', '242,235', '121,252'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Math/CountingBits.java b/leetcode_java/src/main/java/LeetCodeJava/Math/CountingBits.java index a0e15037..b472baeb 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Math/CountingBits.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Math/CountingBits.java @@ -29,6 +29,37 @@ private int countElement(String s, char x){ return count; } + // V0 + // IDEA : java default + public int[] countBits_0(int n) { + + if (n == 0){ + return new int[]{0}; + } + + int[] res = new int[n+1]; + + for (int i = 0; i < n+1; i++){ + /** + * Integer.bitCount + * + * -> java default get number of "1" from binary representation of a 10 based integer + * + * -> e.g. + * Integer.bitCount(0) = 0 + * Integer.bitCount(1) = 1 + * Integer.bitCount(2) = 1 + * Integer.bitCount(3) = 2 + * + * Ref + * - https://blog.csdn.net/weixin_42092787/article/details/106607426 + */ + int cnt = Integer.bitCount(i); + res[i] = cnt; + } + return res; + } + // V1 // https://leetcode.com/problems/counting-bits/solutions/3987279/java-solution-using-right-shift-operator/ public int countOnes(int n) { diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index 30aa190e..bfd658c3 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -35,30 +35,44 @@ public static void main(String[] args) { - // 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); +// // 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()); +// } - // Print elements from PQs - System.out.println("Small PQ (min-heap):"); - while (!smallPQ.isEmpty()) { - System.out.println(smallPQ.poll()); - } + for (int i = 0; i <= 3; i++){ + int b_cnt = Integer.bitCount(i); - System.out.println("Big PQ (max-heap):"); - while (!bigPQ.isEmpty()) { - System.out.println(bigPQ.poll()); + /** + * + * i = 0 , b_cnt = 0 + * i = 1 , b_cnt = 1 + * i = 2 , b_cnt = 1 + * i = 3 , b_cnt = 2 + * + */ + System.out.println("i = " + i + " , b_cnt = " + b_cnt); } } @@ -739,6 +753,32 @@ public boolean search_sub(String word, TrieNode trie) { } + // LC 338 + public int[] countBits(int n) { + if (n == 0){ + return new int[]{0}; + } + + int[] res = new int[n+1]; + + for (int i = 0; i < n+1; i++){ + /** + * Integer.bitCount + * + * -> java default get number of "1" from binary representation of a 10 based integer + * + * -> e.g. + * Integer.bitCount(0) = 0 + * Integer.bitCount(1) = 1 + * Integer.bitCount(2) = 1 + * Integer.bitCount(3) = 2 + */ + int cnt = Integer.bitCount(i); + res[i] = cnt; + } + return res; + } + }