diff --git a/README.md b/README.md index 7be38ef0..4ff2a348 100644 --- a/README.md +++ b/README.md @@ -870,7 +870,7 @@ 275| [H-Index II](https://leetcode.com/problems/h-index-ii/) | [Python](./leetcode_python/Binary_Search/h-index-ii.py) | _O(logn)_ | _O(1)_ | Medium |similar as `# 274 H-Index`, Binary Search, `fb` | AGAIN**** (3) 278| [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [Python](./leetcode_python/Binary_Search/first-bad-version.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FirstBadVersion.java)| _O(logn)_ | _O(1)_ | Easy | `good basic`,LintCode, binary search, `fb` | OK*** (5) (MUST) 300| [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) | [Python](./leetcode_python/Binary_Search/longest-increasing-subsequence.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/LongestIncreasingSubsequence.java)| _O(nlogn)_ | _O(n)_ | Medium |Curated Top 75, array, binary search,`DP good basic`, LintCode, DP,`amazon`,`fb`| AGAIN********** (10) -315| [Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/description/) | [Python](./leetcode_python/Binary_Search/count_of_smaller_numbers_after_self.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/countOfSmallerNumbersAfterSelf.java)| _O(logn)_ | _O(1)_ | Hard | binary search, BST, BIT, `google` | again* +315| [Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/description/) | [Python](./leetcode_python/Binary_Search/count_of_smaller_numbers_after_self.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/CountOfSmallerNumbersAfterSelf.java)| _O(logn)_ | _O(1)_ | Hard | binary search, BST, BIT, `google` | again* 367| [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/)| [Python](./leetcode_python/Binary_Search/valid-perfect-square.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/ValidPerfectSquare.java) | _O(logn)_ | _O(1)_| Easy | good basic, similar as `# 69 Sqrt(x)` | OK* (3) 374| [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)| [Python](./leetcode_python/Binary_Search/guess-number-higher-or-lower.py)| _O(logn)_ | _O(1)_| Easy| | OK* 410| [Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/)| [Python](./leetcode_python/Binary_Search/split-array-largest-sum.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/SplitArrayLargestSum.java)| _O(logn)_| _O(1)_| Hard| google| AGAIN (not start) diff --git a/data/progress.md b/data/progress.md index db1f9dcc..70cbae36 100644 --- a/data/progress.md +++ b/data/progress.md @@ -1,5 +1,8 @@ # Progress +# 2024-09-07 +- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf + # 2024-08-31 - https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf diff --git a/data/progress.txt b/data/progress.txt index 1920599d..fcb5d8a9 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20240907: 315 20240831: 1110, 1055 20240824: 359,1057,1055(todo) 20240810: 315 diff --git a/data/to_review.txt b/data/to_review.txt index 0458ae22..928329c1 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,11 +1,11 @@ 2024-10-25 -> ['1110, 1055'] 2024-10-18 -> ['359,1057,1055(todo)'] -2024-10-04 -> ['1110, 1055', '315'] +2024-10-04 -> ['315', '1110, 1055'] 2024-09-27 -> ['359,1057,1055(todo)'] 2024-09-21 -> ['1110, 1055'] 2024-09-15 -> ['809'] 2024-09-14 -> ['359,1057,1055(todo)'] -2024-09-13 -> ['1110, 1055', '315'] +2024-09-13 -> ['315', '1110, 1055'] 2024-09-08 -> ['1110, 1055'] 2024-09-06 -> ['359,1057,1055(todo)'] 2024-09-05 -> ['1110, 1055'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/CountOfSmallerNumbersAfterSelf.java b/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/CountOfSmallerNumbersAfterSelf.java index 99954786..8489e3ac 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/CountOfSmallerNumbersAfterSelf.java +++ b/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/CountOfSmallerNumbersAfterSelf.java @@ -4,7 +4,7 @@ import java.util.*; -public class countOfSmallerNumbersAfterSelf { +public class CountOfSmallerNumbersAfterSelf { // V0 // IDEA : BINARY SEARCH (fixed by GPT) @@ -16,6 +16,13 @@ public List countSmaller(int[] nums) { for (int i = nums.length - 1; i >= 0; i--) { int pos = findInsertPosition_(sortedList, nums[i]); res.add(pos); + /** + * NOTE !!! insert op below + * + * syntax : + * + * public abstract void add(int index, E element ) + */ sortedList.add(pos, nums[i]); } @@ -28,6 +35,29 @@ private int findInsertPosition_(List sortedList, int target) { int left = 0; int right = sortedList.size(); + /** + * Log for below: + * + * exp 1 : nums = [5,2,6,1] + * + * sortedList = [] + * sortedList = [1] + * sortedList = [1, 6] + * sortedList = [1, 2, 6] + * + * + * exp 2 : nums = [-1] + * + * sortedList = [] + * + * + * exp 3 : nums = [-1,-1] + * + * sortedList = [] + * sortedList = [-1] + */ + System.out.println("sortedList = " + sortedList); + while (left < right) { int mid = left + (right - left) / 2; if (sortedList.get(mid) >= target) { diff --git a/leetcode_java/src/main/java/dev/workSpace4.java b/leetcode_java/src/main/java/dev/workSpace4.java index ea4f4523..06cd3cc9 100644 --- a/leetcode_java/src/main/java/dev/workSpace4.java +++ b/leetcode_java/src/main/java/dev/workSpace4.java @@ -392,6 +392,20 @@ public int compare(Integer o1, Integer o2) { // >>> x_1 = [a, b] System.out.println(">>> x_1 = " + x_1.toString()); + System.out.println("copyOfRange --------------"); + +// List arr_100 = new ArrayList<>(); +// arr_100.add(1); +// arr_100.add(2); +// arr_100.add(3); + int[] arr_100 = new int[]{1,2,3}; + // int[] nums_ = Arrays.copyOfRange(nums, idx, nums.length); + //System.out.println(Arrays.copyOfRange(arr_100, 1, arr_100.length).toString()); + int[] copy = Arrays.copyOfRange(arr_100, 1, arr_100.length); + for (int val : copy){ + System.out.println(val); + } + } diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index 48184b83..6a3165df 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -6655,6 +6655,58 @@ private Map getCountMap(String s){ // LC 315 // 6.28 public List countSmaller(int[] nums) { + List res = new ArrayList<>(); + if (nums == null || nums.length == 0){ + return null; + } + if (nums.length == 1){ + res.add(0); + return res; + } + // brute force + for (int i = 0; i < nums.length; i++){ +// int cnt = 0; +// for (int j = i+1; j < nums.length; j++){ +// if (nums[i] > nums[j]){ +// cnt += 1; +// } +// } +// res.add(cnt); + int cnt = findSmallerCnt(i, nums); + System.out.println(">>> cnt = " + cnt + ", i = " + i + " , nums = " + nums); + res.add(cnt); + } + + return res; + } + + private int findSmallerCnt(int idx, int[] nums){ + int cnt = 0; + // binary search + int toCompare = nums[idx]; + int[] nums_ = Arrays.copyOfRange(nums, idx+1, nums.length); + Arrays.sort(nums_); + int l = 0; + int r = nums_.length; + int mid = (l + r) / 2; + while (r > l){ + System.out.println("l = " + l + ", r = " + r + ", mid = " + mid); + // found + if (toCompare < nums[mid+1] && toCompare > nums[mid-1]){ + return nums_.length - mid; + } + if (toCompare > nums[mid]){ + l = mid + 1; + }else{ + r = mid; + } + } + + return cnt; + } + + /////// + public List countSmaller_1(int[] nums) { List res = new ArrayList<>();