From 8a823a8da7d94c84d837939ec15d4e0ef0779153 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Wed, 6 Dec 2023 17:39:48 +0800 Subject: [PATCH] update 003 java, progress --- README.md | 2 +- data/progress.txt | 1 + data/to_review.txt | 12 +- ...stSubstringWithoutRepeatingCharacters.java | 41 ++++- leetcode_java/src/main/java/dev/Test1.java | 171 +++++++++++++++++- 5 files changed, 218 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5b41bd19..af2d796e 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/data/progress.txt b/data/progress.txt index 2a0c9ad8..6e33ef3e 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20231206: 19,003 20231205: 23 20231204: 55,45 20231108: 452,406,135 diff --git a/data/to_review.txt b/data/to_review.txt index dfc5a431..ac68be08 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/HashTable/LongestSubstringWithoutRepeatingCharacters.java b/leetcode_java/src/main/java/LeetCodeJava/HashTable/LongestSubstringWithoutRepeatingCharacters.java index be273a17..3255739b 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/HashTable/LongestSubstringWithoutRepeatingCharacters.java +++ b/leetcode_java/src/main/java/LeetCodeJava/HashTable/LongestSubstringWithoutRepeatingCharacters.java @@ -10,9 +10,48 @@ public class LongestSubstringWithoutRepeatingCharacters { // V0 - // IDEA : SLIDING WINDOW + HASH SET + // IDEA : HASHMAP + SLIDING DINDOW public int lengthOfLongestSubstring(String s) { + Map 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; } diff --git a/leetcode_java/src/main/java/dev/Test1.java b/leetcode_java/src/main/java/dev/Test1.java index bc0269a8..1095dfa9 100644 --- a/leetcode_java/src/main/java/dev/Test1.java +++ b/leetcode_java/src/main/java/dev/Test1.java @@ -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 { @@ -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 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 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; + } + }