Skip to content

Commit

Permalink
add 315 java, py, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Aug 10, 2024
1 parent aa73246 commit 49ac8a2
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,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*
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
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240810: 642
20240810: 315
20240722: 809
20240708: 1055(todo),482(again)
20240707: 1170(again)
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
2024-10-04 -> ['642']
2024-10-04 -> ['315']
2024-09-15 -> ['809']
2024-09-13 -> ['642']
2024-09-13 -> ['315']
2024-09-01 -> ['1055(todo),482(again)']
2024-08-31 -> ['642', '1170(again)']
2024-08-31 -> ['315', '1170(again)']
2024-08-30 -> ['410(todo),843(todo),222']
2024-08-27 -> ['1007']
2024-08-26 -> ['3196']
2024-08-25 -> ['809']
2024-08-23 -> ['642', '3195']
2024-08-23 -> ['315', '3195']
2024-08-20 -> ['3194']
2024-08-19 -> ['49,347,128,234,121,3(todo)']
2024-08-18 -> ['642', '3192,2357']
2024-08-18 -> ['315', '3192,2357']
2024-08-17 -> ['3190,3191']
2024-08-16 -> ['684(again!!),79']
2024-08-15 -> ['642']
2024-08-15 -> ['315']
2024-08-14 -> ['2013']
2024-08-13 -> ['642', '48']
2024-08-12 -> ['642', '809']
2024-08-11 -> ['642', '1055(todo),482(again)']
2024-08-13 -> ['315', '48']
2024-08-12 -> ['315', '809']
2024-08-11 -> ['315', '1055(todo),482(again)']
2024-08-10 -> ['1170(again)']
2024-08-09 -> ['410(todo),843(todo),222']
2024-08-06 -> ['1007']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
package LeetCodeJava.BinarySearch;

// https://leetcode.com/problems/count-of-smaller-numbers-after-self/description/

import java.util.*;

public class countOfSmallerNumbersAfterSelf {

// V0
// TODO : implement

// V1
// IDEA : BINARY SEARCH (GPT)
/**
* 1. Sorted List:
* • We maintain a sortedList that keeps track of the elements we’ve encountered so far, sorted in ascending order.
*
* 2. Binary Search for Insertion:
* • For each element in the input array, starting from the end (rightmost element):
* • We use binary search to find the correct position where the current element should be inserted into sortedList.
* • The index at which we would insert the current element gives us the count of elements that are smaller than the current element and have been encountered so far.
*
* 3. Insert Element:
* • After determining the insert position, we insert the current element into the sortedList at that position. This keeps the list sorted for subsequent operations.
*
* 4. Result Array:
* • We store the count of smaller elements to the right in the result array, which is eventually returned as a list.
*
* 5. Time Complexity:
* • The time complexity of this approach is O(n log n) where n is the length of the input array. The log n factor comes from the binary search, and we do this for each of the n elements.
*
*/
public List<Integer> countSmaller_1(int[] nums) {
List<Integer> sortedList = new ArrayList<>();
Integer[] result = new Integer[nums.length];

for (int i = nums.length - 1; i >= 0; i--) {
int currentNumber = nums[i];
// Find the position where currentNumber should be inserted
int insertPosition = findInsertPosition(sortedList, currentNumber);

// The insert position gives the count of smaller elements to the right
result[i] = insertPosition;

// Insert currentNumber in the sorted list
sortedList.add(insertPosition, currentNumber);
}

return Arrays.asList(result);
}

// Helper method to find the insert position using binary search
private int findInsertPosition(List<Integer> sortedList, int target) {
int left = 0;
int right = sortedList.size();

while (left < right) {
int mid = left + (right - left) / 2;
// sortedList[mid] >= target
if (sortedList.get(mid) >= target) {
right = mid;
}
// sortedList[mid] < target
else {
left = mid + 1;
}
}

return left;
}

// V2
// IDEA : Binary Index Tree (BIT)
// https://leetcode.com/problems/count-of-smaller-numbers-after-self/solutions/76611/short-java-binary-index-tree-beat-97-33-with-detailed-explanation/
// https://www.topcoder.com/thrive/articles/Binary%20Indexed%20Trees
/**
* IDEA :
*
* 1, we should build an array with the length equals to the max element of the nums array as BIT.
* 2, To avoid minus value in the array, we should first add the (min+1) for every elements
* (It may be out of range, where we can use long to build another array. But no such case in the test cases so far.)
* 3, Using standard BIT operation to solve it.
*
*/
public List<Integer> countSmaller_2(int[] nums) {
List<Integer> res = new LinkedList<Integer>();
if (nums == null || nums.length == 0) {
return res;
}
// find min value and minus min by each elements, plus 1 to avoid 0 element
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
min = (nums[i] < min) ? nums[i]:min;
}
int[] nums2 = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
nums2[i] = nums[i] - min + 1;
max = Math.max(nums2[i],max);
}
int[] tree = new int[max+1];
for (int i = nums2.length-1; i >= 0; i--) {
res.add(0,get(nums2[i]-1,tree));
update(nums2[i],tree);
}
return res;
}
private int get(int i, int[] tree) {
int num = 0;
while (i > 0) {
num +=tree[i];
i -= i&(-i);
}
return num;
}
private void update(int i, int[] tree) {
while (i < tree.length) {
tree[i] ++;
i += i & (-i);
}
}


// V3
// IDEA : BST
// TODO : fix TLE
// https://leetcode.com/problems/count-of-smaller-numbers-after-self/solutions/76587/easiest-java-solution/
public List<Integer> countSmaller_3(int[] nums) {
List<Integer> res = new ArrayList<>();
if(nums == null || nums.length == 0) return res;
TreeNode root = new TreeNode(nums[nums.length - 1]);
res.add(0);
for(int i = nums.length - 2; i >= 0; i--) {
int count = insertNode(root, nums[i]);
res.add(count);
}
Collections.reverse(res);
return res;
}

public int insertNode(TreeNode root, int val) {
int thisCount = 0;
while(true) {
if(val <= root.val) {
root.count++;
if(root.left == null) {
root.left = new TreeNode(val); break;
} else {
root = root.left;
}
} else {
thisCount += root.count;
if(root.right == null) {
root.right = new TreeNode(val); break;
} else {
root = root.right;
}
}
}
return thisCount;
}
}

class TreeNode {
TreeNode left;
TreeNode right;
int val;
int count = 1;
public TreeNode(int val) {
this.val = val;
}


// V4
// https://leetcode.com/problems/count-of-smaller-numbers-after-self/solutions/445769/merge-sort-clear-simple-explanation-with-examples-o-n-lg-n/
//
// Wrapper class for each and every value of the input array,
// to store the original index position of each value, before we merge sort the array
private class ArrayValWithOrigIdx {
int val;
int originalIdx;

public ArrayValWithOrigIdx(int val, int originalIdx) {
this.val = val;
this.originalIdx = originalIdx;
}
}

public List<Integer> countSmaller_4(int[] nums) {
if (nums == null || nums.length == 0) return new LinkedList<Integer>();
int n = nums.length;
int[] result = new int[n];

ArrayValWithOrigIdx[] newNums = new ArrayValWithOrigIdx[n];
for (int i = 0; i < n; ++i) newNums[i] = new ArrayValWithOrigIdx(nums[i], i);

mergeSortAndCount(newNums, 0, n - 1, result);

// notice we don't care about the sorted array after merge sort finishes.
// we only wanted the result counts, generated by running merge sort
List<Integer> resultList = new LinkedList<Integer>();
for (int i : result) resultList.add(i);
return resultList;
}

private void mergeSortAndCount(ArrayValWithOrigIdx[] nums, int start, int end, int[] result) {
if (start >= end) return;

int mid = (start + end) / 2;
mergeSortAndCount(nums, start, mid, result);
mergeSortAndCount(nums, mid + 1, end, result);

// left subarray start...mid
// right subarray mid+1...end
int leftPos = start;
int rightPos = mid + 1;
LinkedList<ArrayValWithOrigIdx> merged = new LinkedList<ArrayValWithOrigIdx>();
int numElemsRightArrayLessThanLeftArray = 0;
while (leftPos < mid + 1 && rightPos <= end) {
if (nums[leftPos].val > nums[rightPos].val) {
// this code block is exactly what the problem is asking us for:
// a number from the right side of the original input array, is smaller
// than a number from the left side
//
// within this code block,
// nums[rightPos] is smaller than the start of the left sub-array.
// Since left sub-array is already sorted,
// nums[rightPos] must also be smaller than the entire remaining left sub-array
++numElemsRightArrayLessThanLeftArray;

// continue with normal merge sort, merge
merged.add(nums[rightPos]);
++rightPos;
} else {
// a number from left side of array, is smaller than a number from
// right side of array
result[nums[leftPos].originalIdx] += numElemsRightArrayLessThanLeftArray;

// Continue with normal merge sort
merged.add(nums[leftPos]);
++leftPos;
}
}

// part of normal merge sort, if either left or right sub-array is not empty,
// move all remaining elements into merged result
while (leftPos < mid + 1) {
result[nums[leftPos].originalIdx] += numElemsRightArrayLessThanLeftArray;

merged.add(nums[leftPos]);
++leftPos;
}
while (rightPos <= end) {
merged.add(nums[rightPos]);
++rightPos;
}

// part of normal merge sort
// copy back merged result into array
int pos = start;
for (ArrayValWithOrigIdx m : merged) {
nums[pos] = m;
++pos;
}
}

}
8 changes: 8 additions & 0 deletions leetcode_java/src/main/java/dev/workSpace4.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@ public int compare(Integer o1, Integer o2) {
String alphabetsReversed = sb.reverse().toString();
System.out.println("alphabetsReversed = " + alphabetsReversed);


System.out.println("reverse loop --------------");
int[] mydata = new int[]{1,2,3};
for (int i = mydata.length-1; i >= 0; i--){
System.out.println(mydata[i]);
}


}

public class Person implements Comparable<Person>{
Expand Down
Loading

0 comments on commit 49ac8a2

Please sign in to comment.