Skip to content

Commit

Permalink
update 003 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 6, 2023
1 parent cdc715f commit 8a823a8
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@
| # | Title | Solution | Time | Space | Difficulty | Status | Note|
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
001| [Two Sum](https://leetcode.com/problems/two-sum/)| [Python](./leetcode_python/Hash_table/two-sum.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/TwoSum.java),[Scala](./leetcode_scala/Hash_table/twoSum.scala) | _O(n)_| _O(n)_ | Easy |`good basic`, `hash table`, `apple`, `amazon`, `UBER`, `grab`, `fb`| OK*** (6) (but again)
003| [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Python](./leetcode_python/Hash_table/longest-substring-without-repeating-characters.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/LongestSubstringWithoutRepeatingCharacters.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, 2 pointers, optimized (SLIDING WINDOW + DICT),`good trick`, `hashMap`, `apple`,`amazon`,`fb`| AGAIN**************** (9) (but AGAIN)
003| [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Python](./leetcode_python/Hash_table/longest-substring-without-repeating-characters.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/LongestSubstringWithoutRepeatingCharacters.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, 2 pointers, optimized (SLIDING WINDOW + DICT),`good trick`, `hashMap`, `apple`,`amazon`,`fb`| AGAIN**************** (10) (but AGAIN)
036| [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Python](./leetcode_python/Hash_table/valid-sudoku.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/ValidSudoku.java) | _O(9^2)_ | _O(9)_ | Medium |array, hashset, `UBER`, `apple`, `amazon`| OK**** (3) (again)
049| [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [Python](./leetcode_python/Hash_table/group-anagrams.py), [Scala](./leetcode_scala/Hash_table/group-anagrams.scala), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/GroupAnagrams.java) | _O(n * glogg)_ | _O(n)_ | Medium |Curated Top 75, `good basic`, dict,`trick`,`dict + sort`, `UBER`, `amazon`, `fb`| OK ********** (6)
170| [Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/) | [Python ](./leetcode_python/Hash_table/two-sum-iii-data-structure-design.py) | _O(n)_ | _O(n)_ | Easy | 🔒, `trick`,`good basic`, `linkedin`, `fb`| OK*** (4)
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 @@
20231206: 19,003
20231205: 23
20231204: 55,45
20231108: 452,406,135
Expand Down
12 changes: 9 additions & 3 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
2024-01-30 -> ['19,003']
2024-01-29 -> ['23']
2024-01-28 -> ['55,45']
2024-01-09 -> ['19,003']
2024-01-08 -> ['23']
2024-01-07 -> ['55,45']
2024-01-02 -> ['452,406,135']
2023-12-31 -> ['134']
2023-12-28 -> ['53']
2023-12-27 -> ['19,003']
2023-12-26 -> ['23']
2023-12-25 -> ['55,45']
2023-12-19 -> ['19,003']
2023-12-18 -> ['23']
2023-12-17 -> ['55,45']
2023-12-14 -> ['19,003']
2023-12-13 -> ['23']
2023-12-12 -> ['55,45', '452,406,135']
2023-12-11 -> ['19,003']
2023-12-10 -> ['23', '134']
2023-12-09 -> ['55,45']
2023-12-08 -> ['23']
2023-12-07 -> ['23', '55,45', '53']
2023-12-09 -> ['19,003', '55,45']
2023-12-08 -> ['19,003', '23']
2023-12-07 -> ['19,003', '23', '55,45', '53']
2023-12-06 -> ['23', '55,45']
2023-12-05 -> ['55,45']
2023-11-30 -> ['quick_sort,286']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,48 @@
public class LongestSubstringWithoutRepeatingCharacters {

// V0
// IDEA : SLIDING WINDOW + HASH SET
// IDEA : HASHMAP + SLIDING DINDOW
public int lengthOfLongestSubstring(String s) {

Map<String, Integer> map = new HashMap();

// left pointer
int left = 0;
// right pointer
int right = 0;
// final result
int res = 0;

while (right < s.length()) {

String cur = String.valueOf(s.charAt(right));

// NOTE !!! we add element to map first
if (map.containsKey(cur)){
map.put(cur, map.get(cur)+1);
}else{
map.put(cur, 1);
}

// NOTE !!! if map has element -> duplicated elelment
// -> keep remove count from element and move left pointer to right
while (map.get(cur) > 1) {
String l = String.valueOf(s.charAt(left));
map.put(l, map.get(l) - 1);
left += 1;
}

res = Math.max(res, right - left + 1);

right += 1;
}
return res;
}

// V0'
// IDEA : SLIDING WINDOW + HASH SET
public int lengthOfLongestSubstring_(String s) {

if (s.equals("")){
return 0;
}
Expand Down
171 changes: 167 additions & 4 deletions leetcode_java/src/main/java/dev/Test1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import LeetCodeJava.DataStructure.ListNode;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;

public class Test1 {

Expand Down Expand Up @@ -73,4 +70,170 @@ public static void main(String[] args) {
System.out.println(_array);
}

class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}


// https://leetcode.com/problems/remove-nth-node-from-end-of-list/
// idea : slow - fast pointers
// NOTE !!! Given the head of a linked list, remove the nth node from the end of the list and return its head.
// FROM THE END
public ListNode removeNthFromEnd(ListNode head, int n) {

if (head == null || head.next == null){
return null;
}

if (n < 0){
return null;
}

// ListNode head_ = head;
// int len = 0;
// while (head_ != null){
// len += 1;
// head_ = head_.next;
// }

ListNode dummy = new ListNode(0);

dummy.next = head;

ListNode slow = dummy;
ListNode fast = dummy;

// move fast n+1 steps
//int j = len - n;
for (int i = 0; i < n; i++){
fast = fast.next;
}

// move fast, and slow on the same time, till fast reach the end of listNode
while (fast != null){
fast = fast.next;
slow = slow.next;
}

// return slow as result
slow = slow.next.next;
return dummy.next;

//return null;
}

/**
* "abcabcbb"
* lr
*
* "abcabcbb"
* l r
*
* "abcabcbb"
* l r
*
* "abcabcbb"
* l r
*
*
* "pwwkew"
* lr
*
* "pwwkew"
* l r
*
* "pwwkew"
* lr
*
* "pwwkew"
* l
* r
*
* "pwwkew"
* l
* r
*
*
* 5:25
*
*/

// https://leetcode.com/problems/longest-substring-without-repeating-characters/
// IDEA : sliding window + hashset
public int lengthOfLongestSubstring(String s) {

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

HashSet<String> set = new HashSet<>();
int l = 0;
int ans = 1;
int r = 0;
while (r < s.length() && l < s.length()){
System.out.println("r = " + r + " l = " + l + " set = " + set + " ans = " + ans);
String cur = String.valueOf(s.charAt(r));
if (!set.contains(cur)){
set.add(cur);
//ans += 1;
ans = Math.max(ans, r - l + 1);
r += 1;
//continue;
}else{
while (set.contains(cur) && l < r){
// keep moving left pointer, if sub array element still in set
l += 1;
}
// finally remove cur from set
set.remove(cur);
}
}

return ans;
}

// V2
// IDEA : SLIDING WINDOW
// https://leetcode.com/problems/longest-substring-without-repeating-characters/editorial/
public int lengthOfLongestSubstring_3(String s) {

Map<String, Integer> map = new HashMap();

// left pointer
int left = 0;
// right pointer
int right = 0;
// final result
int res = 0;

while (right < s.length()) {

String cur = String.valueOf(s.charAt(right));

// NOTE !!! we add element to map first
if (map.containsKey(cur)){
map.put(cur, map.get(cur)+1);
}else{
map.put(cur, 1);
}

// NOTE !!! if map has element -> duplicated elelment
// -> keep remove count from element and move left pointer to right
while (map.get(cur) > 1) {
String l = String.valueOf(s.charAt(left));
map.put(l, map.get(l) - 1);
left += 1;
}

res = Math.max(res, right - left + 1);

right += 1;
}
return res;
}

}

0 comments on commit 8a823a8

Please sign in to comment.