From 112a38b0216620249e270c771fc70e2d26fdec6f Mon Sep 17 00:00:00 2001 From: GangBean Date: Wed, 29 Jan 2025 12:20:28 +0900 Subject: [PATCH 01/10] feat: solve design add and search words data structure --- .../GangBean.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 design-add-and-search-words-data-structure/GangBean.java diff --git a/design-add-and-search-words-data-structure/GangBean.java b/design-add-and-search-words-data-structure/GangBean.java new file mode 100644 index 000000000..8488ad42d --- /dev/null +++ b/design-add-and-search-words-data-structure/GangBean.java @@ -0,0 +1,47 @@ +class WordDictionary { + private Map children; + private boolean isEnd; + + public WordDictionary() { + this.children = new HashMap<>(); + this.isEnd = false; + } + + public void addWord(String word) { + char[] arr = word.toCharArray(); + WordDictionary next = this; + for (int i = 0; i < arr.length; i++) { + char c = arr[i]; + next.children.putIfAbsent(c, new WordDictionary()); + next = next.children.get(c); + } + next.isEnd = true; + } + + public boolean search(String word) { + // System.out.println(this); + return search(word, 0); + } + + private boolean search(String word, int idx) { + if (idx == word.length()) return this.isEnd; + char c = word.charAt(idx); + if (c == '.') { + return this.children.values().stream().anyMatch(child -> child.search(word, idx+1)); + } + if (!this.children.containsKey(c)) return false; + return this.children.get(c).search(word, idx+1); + } + + public String toString() { + return String.format("%s -> %s", isEnd, this.children); + } +} + +/** + * Your WordDictionary object will be instantiated and called as such: + * WordDictionary obj = new WordDictionary(); + * obj.addWord(word); + * boolean param_2 = obj.search(word); + */ + From 114f46c6b785181256a4f2cb211adaab61a30f8e Mon Sep 17 00:00:00 2001 From: GangBean Date: Wed, 29 Jan 2025 14:15:34 +0900 Subject: [PATCH 02/10] feat: solve spiral matrix --- spiral-matrix/GangBean.java | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 spiral-matrix/GangBean.java diff --git a/spiral-matrix/GangBean.java b/spiral-matrix/GangBean.java new file mode 100644 index 000000000..8dc5d1a59 --- /dev/null +++ b/spiral-matrix/GangBean.java @@ -0,0 +1,37 @@ +class Solution { + /** + 1. understanding + - 3 x 3 = N x M + - (1,2,3) -> (6,9) // (8,7) -> (4) // (5) + - upper: step M count, N -= 1 // right: step N count, M -= 1 // bottom: step M count, N -= 1 // left: step N count, M -= 1 + 2. complexity: + - time: O(N * M) + - space: O(1) + */ + public List spiralOrder(int[][] matrix) { + int r = 0; + int c = -1; + int dir = 1; + int N = matrix.length; + int M = matrix[0].length; + List ret = new ArrayList<>(); + + while (0 < N && 0 < M) { + for (int i = 0; i < M; i++) { + c += dir; + ret.add(matrix[r][c]); + } + N -= 1; + for (int i = 0; i < N; i++) { + r += dir; + ret.add(matrix[r][c]); + } + M -= 1; + + dir *= -1; + } + + return ret; + } +} + From 0898647ab69cfc696c5c06b50e42fbb10314a8e8 Mon Sep 17 00:00:00 2001 From: GangBean Date: Fri, 31 Jan 2025 10:09:55 +0900 Subject: [PATCH 03/10] feat: solve longest increasing subsequence --- longest-increasing-subsequence/GangBean.java | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 longest-increasing-subsequence/GangBean.java diff --git a/longest-increasing-subsequence/GangBean.java b/longest-increasing-subsequence/GangBean.java new file mode 100644 index 000000000..78c364e5a --- /dev/null +++ b/longest-increasing-subsequence/GangBean.java @@ -0,0 +1,22 @@ +class Solution { + /** + 1. understanding + - dp[n] : length of longest increasing subsequence in 0...n + 2. space + - time: O(N^2) + - space: O(N) + */ + public int lengthOfLIS(int[] nums) { + int[] dp = new int[nums.length]; + Arrays.fill(dp, 1); + for (int i = 0; i < nums.length; i++) { // O(N) + for (int j = 0; j <= i; j++) { // O(N) + if (nums[j] < nums[i]) { + dp[i] = Math.max(dp[j]+1, dp[i]); + } + } + } + return Arrays.stream(dp).max().orElse(1); + } +} + From ff209e66265cc93ff361aa1600710f34ce1b6103 Mon Sep 17 00:00:00 2001 From: GangBean Date: Fri, 31 Jan 2025 10:15:27 +0900 Subject: [PATCH 04/10] feat: solve reverse linked list --- reverse-linked-list/GangBean.java | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 reverse-linked-list/GangBean.java diff --git a/reverse-linked-list/GangBean.java b/reverse-linked-list/GangBean.java new file mode 100644 index 000000000..ce9b98e5d --- /dev/null +++ b/reverse-linked-list/GangBean.java @@ -0,0 +1,42 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + /** + 1. strategy + - iterate over all Node, save value to list + - iterate list in reverse order, create Node and link + 2. complexity + - time: O(N) + - space: O(N) + */ + public ListNode reverseList(ListNode head) { + List list = new ArrayList<>(); + + while (head != null) { + list.add(head.val); + head = head.next; + } + + ListNode prev = new ListNode(); + ListNode ret = null; + + for (int i=list.size()-1; i>=0; i--) { + ListNode node = new ListNode(list.get(i)); + prev.next = node; + prev = prev.next; + if (ret == null) { + ret = prev; + } + } + return ret; + } +} + From a9b017c186b28f72de31040807f02879640de482 Mon Sep 17 00:00:00 2001 From: GangBean Date: Fri, 31 Jan 2025 10:23:45 +0900 Subject: [PATCH 05/10] feat: solve longest substring without repeating characters --- .../GangBean.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 longest-substring-without-repeating-characters/GangBean.java diff --git a/longest-substring-without-repeating-characters/GangBean.java b/longest-substring-without-repeating-characters/GangBean.java new file mode 100644 index 000000000..744e3f159 --- /dev/null +++ b/longest-substring-without-repeating-characters/GangBean.java @@ -0,0 +1,38 @@ +class Solution { + /** + 1. understanding + - [a] -> [a,b] -> [a,b,c] -> [a] -> [a,b] -> [a,b,c] -> [b] -> [b] + 2. complexity + - time: O(N) + - space: O(N) + */ + public int lengthOfLongestSubstring(String s) { + int left = 0; + int right = 0; + int len = s.length(); + HashSet charSet = new HashSet(); + int ret = (len==0)?0:1; + while (left ret) { + ret = tmpLen; + } + right++; + } else { + left++; + right = left; + } + } + } + + return ret; + } +} + From 981f201f0ab3fd75a4c1e784899fe80a672007b1 Mon Sep 17 00:00:00 2001 From: GangBean Date: Fri, 31 Jan 2025 10:31:29 +0900 Subject: [PATCH 06/10] feat: solve number of islands --- number-of-islands/GangBean.java | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 number-of-islands/GangBean.java diff --git a/number-of-islands/GangBean.java b/number-of-islands/GangBean.java new file mode 100644 index 000000000..b385059bd --- /dev/null +++ b/number-of-islands/GangBean.java @@ -0,0 +1,38 @@ +class Solution { + /** + 1. understanding + - for each grid cell, if it is land, then move horizontally and vertically, to find connecting lands + 2. complexity + - time: O(N * M) where grid is N * M matrix + - space: O(1) + */ + public int numIslands(char[][] grid) { + int ret = 0; + for (int i=0; i Date: Fri, 31 Jan 2025 10:57:28 +0900 Subject: [PATCH 07/10] feat: solve unique paths --- unique-paths/GangBean.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 unique-paths/GangBean.java diff --git a/unique-paths/GangBean.java b/unique-paths/GangBean.java new file mode 100644 index 000000000..a5fc7bee9 --- /dev/null +++ b/unique-paths/GangBean.java @@ -0,0 +1,24 @@ +import java.math.BigDecimal; + +class Solution { + /** + 1. understanding + - To reach destination, you have to move bottom direction in m-1 times, and move to right direction in n-1 times. + - in example 2, [(DDR), (DRD), (RDD)] is the paths. + - so, the number of paths are combinations of (m-1) D and (n-1) R + - (m+n-2)!/(m-1)!(n-1)!, where ! means factorial, n! = 1*2*...*n + - factorial[n]: n! + 2. complexity + - time: O(m+n) + - space: O(m+n) + */ + public int uniquePaths(int m, int n) { + BigDecimal[] dp = new BigDecimal[m+n]; + Arrays.fill(dp, BigDecimal.ONE); + for (int num = 2; num < m+n; num++) { + dp[num] = dp[num-1].multiply(new BigDecimal(num)); + } + return dp[m+n-2].divide(dp[m-1]).divide(dp[n-1]).intValue(); + } +} + From 10b20a44a32c8dad12d1bb6dd135dda1ac09831a Mon Sep 17 00:00:00 2001 From: GangBean Date: Fri, 31 Jan 2025 11:06:25 +0900 Subject: [PATCH 08/10] feat: solve set matrix zeroes --- set-matrix-zeroes/GangBean.java | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 set-matrix-zeroes/GangBean.java diff --git a/set-matrix-zeroes/GangBean.java b/set-matrix-zeroes/GangBean.java new file mode 100644 index 000000000..51defb527 --- /dev/null +++ b/set-matrix-zeroes/GangBean.java @@ -0,0 +1,40 @@ +class Solution { + /** + 1. understanding + - iterate over all cells, if value is 0, then add row and col to sweep target set. + - for each row target set, and col target set, sweep all it's value to 0 + 2. complexity + - time: O(m * n) + - space: O(m + n) + */ + public void setZeroes(int[][] matrix) { + Set rows = new HashSet<>(); // O(m) + Set cols = new HashSet<>(); // O(n) + + for (int row = 0; row < matrix.length; row++) { // O(m) + for (int col = 0; col < matrix[row].length; col++) { // O(n) + if (matrix[row][col] == 0) { // O(m * n) + rows.add(row); + cols.add(col); + } + } + } + + for (int row: rows) { // O(m) + int col = 0; + while (col < matrix[row].length) { // O(n) + matrix[row][col] = 0; + col++; + } + } + + for (int col: cols) { // O(n) + int row = 0; + while (row < matrix.length) { // O(m) + matrix[row][col] = 0; + row++; + } + } + } +} + From 6b865f9a6ba00f426d7077538bceb9df1184f0ba Mon Sep 17 00:00:00 2001 From: GangBean Date: Fri, 31 Jan 2025 11:15:52 +0900 Subject: [PATCH 09/10] feat: solve number of 1 bits --- number-of-1-bits/GangBean.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 number-of-1-bits/GangBean.java diff --git a/number-of-1-bits/GangBean.java b/number-of-1-bits/GangBean.java new file mode 100644 index 000000000..b288522c7 --- /dev/null +++ b/number-of-1-bits/GangBean.java @@ -0,0 +1,11 @@ +class Solution { + public int hammingWeight(int n) { + int count = 0; + while (n >= 1) { + count += n % 2; + n /= 2; + } + return count; + } +} + From fcb00e0cda19d9a61b90bc547abecad907e77912 Mon Sep 17 00:00:00 2001 From: GangBean Date: Sat, 1 Feb 2025 06:45:59 +0900 Subject: [PATCH 10/10] refactor: solve reverse linked list with other way --- reverse-linked-list/GangBean.java | 33 ++++++++----------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/reverse-linked-list/GangBean.java b/reverse-linked-list/GangBean.java index ce9b98e5d..8b986b52e 100644 --- a/reverse-linked-list/GangBean.java +++ b/reverse-linked-list/GangBean.java @@ -9,34 +9,17 @@ * } */ class Solution { - /** - 1. strategy - - iterate over all Node, save value to list - - iterate list in reverse order, create Node and link - 2. complexity - - time: O(N) - - space: O(N) - */ public ListNode reverseList(ListNode head) { - List list = new ArrayList<>(); - - while (head != null) { - list.add(head.val); - head = head.next; + ListNode curr = head; + ListNode prev = null; + while (curr != null) { + ListNode next = curr.next; + curr.next = prev; + prev = curr; + curr = next; } - ListNode prev = new ListNode(); - ListNode ret = null; - - for (int i=list.size()-1; i>=0; i--) { - ListNode node = new ListNode(list.get(i)); - prev.next = node; - prev = prev.next; - if (ret == null) { - ret = prev; - } - } - return ret; + return prev; } }