Skip to content

Commit

Permalink
update 019 java, update cheatsheet, progress, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Feb 27, 2024
1 parent 8c8493f commit 13490c5
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@

| # | Title | Solution | Time | Space | Difficulty | Note | Status|
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
019| [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)| [Python](./leetcode_python/Two_Pointers/remove-nth-node-from-end-of-list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/RemoveNthNodeFromEndOfList.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, good basic, `linked list`,`two pointers`,`fb`| AGAIN********** (4) (MUST)
019| [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)| [Python](./leetcode_python/Two_Pointers/remove-nth-node-from-end-of-list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/RemoveNthNodeFromEndOfList.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, good basic, MUST, `linked list`,`two pointers`,`fb`| AGAIN********** (4) (MUST)
042 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | [Python](./leetcode_python/Two_Pointers/trapping-rain-water.py) || | Hard |scan, dp, trick, two pointer, `amazon`, `apple`| AGAIN*** (2) (not start)
086| [Partition List](https://leetcode.com/problems/partition-list/)| [Python](./leetcode_python/Two_Pointers/partition-list.py) | _O(n)_ | _O(1)_ | Medium || OK*
141| [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)| [Python](./leetcode_python/Two_Pointers/linked-list-cycle.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/TwoPointer/LinkedListCycle.java) | _O(n)_ | _O(1)_ | Easy |Curated Top 75, `basic`,`amazon`, `fb`, linked list, 2 pointers, check #142| OK** (5)
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 @@
20240227: 1,3,5
20240227: 1,3,5,4,19
20231218: 57(todo),58(todo),268,61(todo),297
20231211: 39,53,48,56
20231208: 33
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
2024-04-22 -> ['1,3,5']
2024-04-01 -> ['1,3,5']
2024-03-19 -> ['1,3,5']
2024-03-11 -> ['1,3,5']
2024-03-06 -> ['1,3,5']
2024-03-03 -> ['1,3,5']
2024-03-01 -> ['1,3,5']
2024-02-29 -> ['1,3,5']
2024-02-28 -> ['1,3,5']
2024-04-22 -> ['1,3,5,4,19']
2024-04-01 -> ['1,3,5,4,19']
2024-03-19 -> ['1,3,5,4,19']
2024-03-11 -> ['1,3,5,4,19']
2024-03-06 -> ['1,3,5,4,19']
2024-03-03 -> ['1,3,5,4,19']
2024-03-01 -> ['1,3,5,4,19']
2024-02-29 -> ['1,3,5,4,19']
2024-02-28 -> ['1,3,5,4,19']
2024-02-11 -> ['57(todo),58(todo),268,61(todo),297']
2024-02-04 -> ['39,53,48,56']
2024-01-30 -> ['19,003']
Expand Down
64 changes: 64 additions & 0 deletions doc/cheatsheet/linked_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ node2.next = node3;
- check
- check cyclic linked list
- check beginning of cyclic linked list
- remove N th node
- Remove Nth Node From End of List - LC 19
- combinations
- combinations of above cases

Expand Down Expand Up @@ -1026,6 +1028,68 @@ class Solution(object):
return new_head.next
```

```java
// java
public ListNode removeNthFromEnd(ListNode head, int n) {

if (head == null){
return head;
}

if (head.next == null && head.val == n){
return null;
}

// move fast pointer only with n+1 step
// 2 cases:
// - 1) node count is even
// - 2) node count is odd
/** NOTE !! we init dummy pointer, and let fast, slow pointers point to it */
ListNode dummy = new ListNode(0);
dummy.next = head;
// NOTE here
ListNode fast = dummy;
ListNode slow = dummy;
/**
* Explanation V1:
*
* -> So we have fast, and slow pointer,
* if we move fast N steps first,
* then slow starts to move
* -> fast, slow has N step difference
* -> what's more, when fast reach the end,
* -> fast, slow STILL has N step difference
* -> and slow has N step difference with the end,
* -> so we can remove N th pointer accordingly
*
* Explanation V2:
*
*
* // NOTE !!! we let fast pointer move N+1 step first
* // so once fast pointers reach the end after fast, slow pointers move together
* // we are sure that slow pointer is at N-1 node
* // so All we need to do is :
* // point slow.next to slow.next.next
* // then we remove N node from linked list
*/
for (int i = 1; i <= n+1; i++){
//System.out.println("i = " + i);
fast = fast.next;
}

// move fast and slow pointers on the same time
while (fast != null){
fast = fast.next;
slow = slow.next;
}

// NOTE here
slow.next = slow.next.next;
// NOTE !!! we return dummy.next instead of slow
return dummy.next;
}
```

### 2-8) Reorder List
```python
# LC 143. Reorder List
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,42 @@
public class ContainerWithMostWater {

// V0
// IDEA : BRUTE FORCE
// IDEA : 2 POINTERS
public int maxArea(int[] height) {

if (height.length == 0 || height.equals(null)){
return 0;
}

int ans = 0;
int left = 0;
// NOTE : right as height.length - 1
int right = height.length - 1;

// either ">=" or ">" is OK for this problem, but for logic alignment, we use ">=" here
while (right >= left){
int leftVal = height[left];
int rightVal = height[right];
// NOTE !!! right - left, we get distance between left, right pointer
int amount = (right - left) * Math.min(leftVal, rightVal);
ans = Math.max(amount, ans);
if (rightVal > leftVal){
left += 1;
}else{
right -= 1;
}
}
return ans;
}

// V0'
// IDEA : BRUTE FORCE
public int maxArea_1(int[] height) {

if (height.length == 0 || height.equals(null)){
return 0;
}

int ans = 0;
for (int i = 0; i < height.length-1; i++){
for (int j = 1; j < height.length; j++){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,30 @@ public ListNode removeNthFromEnd(ListNode head, int n) {
// NOTE here
ListNode fast = dummy;
ListNode slow = dummy;
// NOTE !!! we let fast pointer move N+1 step first
// so once fast pointers reach the end after fast, slow pointers move together
// we are sure that slow pointer is at N-1 node
// so All we need to do is :
// point slow.next to slow.next.next
// then we remove N node from linked list
/**
* Explanation V1:
*
* -> So we have fast, and slow pointer,
* if we move fast N steps first,
* then slow starts to move
* -> fast, slow has N step difference
* -> what's more, when fast reach the end,
* -> fast, slow STILL has N step difference
* -> and slow has N step difference with the end,
* -> so we can remove N th pointer accordingly
*
* Explanation V2:
*
*
* // NOTE !!! we let fast pointer move N+1 step first
* // so once fast pointers reach the end after fast, slow pointers move together
* // we are sure that slow pointer is at N-1 node
* // so All we need to do is :
* // point slow.next to slow.next.next
* // then we remove N node from linked list
*/
for (int i = 1; i <= n+1; i++){
System.out.println("i = " + i);
//System.out.println("i = " + i);
fast = fast.next;
}

Expand Down

0 comments on commit 13490c5

Please sign in to comment.