diff --git a/longest-substring-without-repeating-characters/minji-go.java b/longest-substring-without-repeating-characters/minji-go.java new file mode 100644 index 000000000..469b143c4 --- /dev/null +++ b/longest-substring-without-repeating-characters/minji-go.java @@ -0,0 +1,28 @@ +/** + * week07-2.longest-substring-without-repeating-characters + *
  • Description: find the length of the longest substring without duplicate characters
  • + *
  • Topics: Hash Table, String, Sliding Window
  • + *
  • Time Complexity: O(N), Runtime 5ms
  • + *
  • Space Complexity: O(L), Memory 44.66MB
  • + */ + +class Solution { + public int lengthOfLongestSubstring(String s) { + int max = 0; + int left = 0; + + Map chars = new HashMap<>(); + for (int right = 0; right < s.length(); right++) { + char c = s.charAt(right); + + if (chars.containsKey(c) && chars.get(c) >= left) { + left = chars.get(c) + 1; + } + + chars.put(c, right); + max = Math.max(max, right - left + 1); + } + + return max; + } +} diff --git a/number-of-islands/minji-go.java b/number-of-islands/minji-go.java new file mode 100644 index 000000000..1be792147 --- /dev/null +++ b/number-of-islands/minji-go.java @@ -0,0 +1,57 @@ +/** + * week07-3. number-of-islands + *
  • Description: return the number of islands is surrounded by water("0")
  • + *
  • Topics: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
  • + *
  • Time Complexity: O(M×N), Runtime 5ms
  • + *
  • Space Complexity: O(M×N), Memory 52.11MB
  • + */ +class Solution { + private char[][] grid; + private int m; + private int n; + private boolean[][] visit; + private int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + + public int numIslands(char[][] grid) { + this.grid = grid; + this.m = grid.length; + this.n = grid[0].length; + this.visit = new boolean[m][n]; + + int num = 0; + for (int r = 0; r < m; r++) { + for (int c = 0; c < n; c++) { + if (grid[r][c] == '1' && !visit[r][c]) { + findIsland(r, c); + num++; + } + } + } + return num; + } + + public void findIsland(int r, int c) { + Queue queue = new LinkedList<>(); + visit[r][c] = true; + queue.offer(new int[]{r, c}); + + while (!queue.isEmpty()) { + int[] cur = queue.poll(); + int cr = cur[0]; + int cc = cur[1]; + + for (int[] dir : directions) { + int nr = cr + dir[0]; + int nc = cc + dir[1]; + if (nr < 0 || nr > m - 1 || nc < 0 || nc > n - 1) { + continue; + } + if (grid[nr][nc] == '1' && !visit[nr][nc]) { + visit[nr][nc] = true; + queue.offer(new int[]{nr, nc}); + } + } + } + } + +} diff --git a/reverse-linked-list/minji-go.java b/reverse-linked-list/minji-go.java new file mode 100644 index 000000000..0a1ddc999 --- /dev/null +++ b/reverse-linked-list/minji-go.java @@ -0,0 +1,21 @@ +/** + * week07-1.reverse-linked-list + *
  • Description: return the reversed list
  • + *
  • Topics: Linked List, Recursion
  • + *
  • Time Complexity: O(N), Runtime 0ms
  • + *
  • Space Complexity: O(1), Memory 43.04MB
  • + */ +class Solution { + public ListNode reverseList(ListNode head) { + ListNode prev = null; + ListNode curr = head; + while (curr != null) { + ListNode temp = curr.next; + curr.next = prev; + prev = curr; + curr = temp; + } + + return prev; + } +} diff --git a/unique-paths/minji-go.java b/unique-paths/minji-go.java new file mode 100644 index 000000000..3d8b06893 --- /dev/null +++ b/unique-paths/minji-go.java @@ -0,0 +1,39 @@ +/** + * week07-4. unique-paths + *
  • Description: return the number of possible unique paths to reach the bottom-right corner
  • + *
  • Topics: Math, Dynamic Programming, Combinatorics
  • + *
  • Time Complexity: O(M×N), Runtime 2ms
  • + *
  • Space Complexity: O(M×N), Memory 40.76MB
  • + */ +class Solution { + public int uniquePaths(int m, int n) { + int[][] dirs = {{1, 0}, {0, 1}}; + int[][] dp = new int[m][n]; + dp[0][0] = 1; + + Queue queue = new LinkedList<>(); + queue.add(new int[]{0, 0}); + + while (!queue.isEmpty()) { + int[] cur = queue.poll(); + int cr = cur[0]; + int cc = cur[1]; + + for (int i = 0; i < 2; i++) { + int nr = cr + dirs[i][0]; + int nc = cc + dirs[i][1]; + + if (nr > m - 1 || nc > n - 1) { + continue; + } + + if (dp[nr][nc] == 0) { + queue.add(new int[]{nr, nc}); + } + dp[nr][nc] += dp[cr][cc]; + } + } + + return dp[m - 1][n - 1]; + } +}