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); + */ + 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); + } +} + 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; + } +} + 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; + } +} + 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 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++; + } + } + } +} + 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; + } +} + 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(); + } +} +