Skip to content

Commit

Permalink
update 315 java, readme, progress, code comment
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 7, 2024
1 parent 48abb03 commit 06c047d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240907: 315
20240831: 1110, 1055
20240824: 359,1057,1055(todo)
20240810: 315
Expand Down
4 changes: 2 additions & 2 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -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']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.util.*;

public class countOfSmallerNumbersAfterSelf {
public class CountOfSmallerNumbersAfterSelf {

// V0
// IDEA : BINARY SEARCH (fixed by GPT)
Expand All @@ -16,6 +16,13 @@ public List<Integer> 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]);
}

Expand All @@ -28,6 +35,29 @@ private int findInsertPosition_(List<Integer> 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) {
Expand Down
14 changes: 14 additions & 0 deletions leetcode_java/src/main/java/dev/workSpace4.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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);
}


}

Expand Down
52 changes: 52 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -6655,6 +6655,58 @@ private Map<String, Integer> getCountMap(String s){
// LC 315
// 6.28
public List<Integer> countSmaller(int[] nums) {
List<Integer> 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<Integer> countSmaller_1(int[] nums) {

List<Integer> res = new ArrayList<>();

Expand Down

0 comments on commit 06c047d

Please sign in to comment.