Skip to content

Commit 564cc96

Browse files
authored
Merge pull request #1633 from minji-go/week14
[minji-go] week 14 solutions
2 parents 3ccf512 + e2e1064 commit 564cc96

File tree

9 files changed

+345
-0
lines changed

9 files changed

+345
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/binary-tree-level-order-traversal/">week14-2. binary-tree-level-order-traversal</a>
3+
* <li>Description: Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level)</li>
4+
* <li>Topics: Tree, Breadth-First Search, Binary Tree</li>
5+
* <li>Time Complexity: O(N), Runtime 1ms </li>
6+
* <li>Space Complexity: O(N), Memory 45.3MB </li>
7+
*/
8+
9+
class Solution {
10+
public List<List<Integer>> levelOrder(TreeNode root) {
11+
if (root == null) {
12+
return Collections.emptyList();
13+
}
14+
15+
Queue<TreeNode> queue = new LinkedList<>();
16+
queue.offer(root);
17+
18+
List<List<Integer>> results = new ArrayList<>();
19+
while (!queue.isEmpty()) {
20+
List<Integer> result = new ArrayList<>();
21+
22+
int size = queue.size();
23+
for (int i = 0; i < size; i++) {
24+
TreeNode node = queue.poll();
25+
result.add(node.val);
26+
if (node.left != null) queue.offer(node.left);
27+
if (node.right != null) queue.offer(node.right);
28+
}
29+
results.add(result);
30+
}
31+
32+
return results;
33+
}
34+
}

counting-bits/minji-go.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/counting-bits/">week14-1. counting-bits</a>
3+
* <li>Description: Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i</li>
4+
* <li>Topics: Dynamic Programming, Bit Manipulation</li>
5+
* <li>Time Complexity: O(N), Runtime 2ms </li>
6+
* <li>Space Complexity: O(N), Memory 46.64MB </li>
7+
*/
8+
class Solution {
9+
public int[] countBits(int n) {
10+
int[] count = new int[n + 1];
11+
12+
int offset = 1;
13+
for (int i = 1; i <= n; i++) {
14+
if (i == offset * 2) offset *= 2;
15+
count[i] = count[i - offset] + 1;
16+
}
17+
18+
return count;
19+
}
20+
}

house-robber-ii/minji-go.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/house-robber-ii/">week14-3. house-robber-ii</a>
3+
* <li>Description: Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police</li>
4+
* <li>Topics: Array, Dynamic Programming </li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 40.6MB</li>
7+
*/
8+
9+
class Solution {
10+
public int rob(int[] nums) {
11+
int n = nums.length;
12+
if (n == 1) return nums[0];
13+
if (n == 2) return Math.max(nums[0], nums[1]);
14+
15+
return Math.max(rob(nums, 0, n - 2), rob(nums, 1, n - 1));
16+
}
17+
18+
public int rob(int[] nums, int start, int end) {
19+
int prev1 = 0, prev2 = 0;
20+
for (int i = start; i <= end; i++) {
21+
int temp = Math.max(prev2, prev1 + nums[i]);
22+
prev1 = prev2;
23+
prev2 = temp;
24+
}
25+
return prev2;
26+
}
27+
}

linked-list-cycle/minji-go.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/linked-list-cycle/">week9-1. linked-list-cycle</a>
3+
* <li>Description: Return true if there is a cycle in the linked list. </li>
4+
* <li>Topics: Hash Table, Linked List, Two Pointers</li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 44.37MB</li>
7+
*/
8+
public class Solution {
9+
public boolean hasCycle(ListNode head) {
10+
if (head == null) {
11+
return false;
12+
}
13+
14+
ListNode slow = head;
15+
ListNode fast = head.next;
16+
17+
while (slow != fast) {
18+
if (fast == null || fast.next == null) {
19+
return false;
20+
}
21+
slow = slow.next;
22+
fast = fast.next.next;
23+
}
24+
25+
return true;
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/maximum-product-subarray/">week9-3. maximum-product-subarray</a>
3+
* <li>Description: Given an integer array nums, find a subarray that has the largest product, and return the product. </li>
4+
* <li>Topics: Array, Dynamic Programming </li>
5+
* <li>Time Complexity: O(N), Runtime 2ms </li>
6+
* <li>Space Complexity: O(1), Memory 45.42MB </li>
7+
*/
8+
class Solution {
9+
public int maxProduct(int[] nums) {
10+
int maxSoFar = nums[0];
11+
int minSoFar = nums[0];
12+
int result = nums[0];
13+
14+
for (int i = 1; i < nums.length; i++) {
15+
int cur = nums[i];
16+
int tempMax = maxSoFar;
17+
18+
maxSoFar = Math.max(cur, Math.max(cur * maxSoFar, cur * minSoFar));
19+
minSoFar = Math.min(cur, Math.min(cur * tempMax, cur * minSoFar));
20+
21+
result = Math.max(result, maxSoFar);
22+
}
23+
24+
return result;
25+
}
26+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/minimum-window-substring/">week9-5. minimum-window-substring</a>
3+
* <li>Description: return the minimum window substring of s such that every character in t (including duplicates) is included in the window </li>
4+
* <li>Topics: Hash Table, String, Sliding Window </li>
5+
* <li>Time Complexity: O(M+N), Runtime 23ms </li>
6+
* <li>Space Complexity: O(1), Memory 45.13MB </li>
7+
*/
8+
class Solution {
9+
public String minWindow(String s, String t) {
10+
if (s.length() < t.length()) return "";
11+
12+
Map<Character, Integer> tmap = new HashMap<>();
13+
for (char c : t.toCharArray()) {
14+
tmap.put(c, tmap.getOrDefault(c, 0) + 1);
15+
}
16+
17+
Map<Character, Integer> smap = new HashMap<>();
18+
int left = 0, right = 0;
19+
int tsize = tmap.size();
20+
int ssize = 0;
21+
int start = -1, end = s.length();
22+
23+
while (right < s.length()) {
24+
char c = s.charAt(right++);
25+
if (tmap.containsKey(c)) {
26+
smap.put(c, smap.getOrDefault(c, 0) + 1);
27+
if (smap.get(c).intValue() == tmap.get(c).intValue()) {
28+
ssize++;
29+
}
30+
}
31+
32+
while (ssize == tsize) {
33+
if (right - left < end - start) {
34+
start = left;
35+
end = right;
36+
}
37+
38+
char l = s.charAt(left++);
39+
if (tmap.containsKey(l)) {
40+
smap.put(l, smap.get(l) - 1);
41+
if (smap.get(l).intValue() < tmap.get(l).intValue()) {
42+
ssize--;
43+
}
44+
}
45+
}
46+
}
47+
48+
return start == -1 ? "" : s.substring(start, end);
49+
}
50+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/pacific-atlantic-water-flow/">week9-2. pacific-atlantic-water-flow</a>
3+
* <li>Description: Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans. </li>
4+
* <li>Topics: Array, Depth-First Search, Breadth-First Search, Matrix</li>
5+
* <li>Time Complexity: O(MN), Runtime 9ms </li>
6+
* <li>Space Complexity: O(MN), Memory 45.18MB</li>
7+
*/
8+
class Solution {
9+
private int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
10+
11+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
12+
int m = heights.length;
13+
int n = heights[0].length;
14+
15+
Queue<int[]> pacificQueue = new LinkedList<>();
16+
boolean[][] pacific = new boolean[m][n];
17+
for (int i = 0; i < m; i++) {
18+
pacific[i][0] = true;
19+
pacificQueue.offer(new int[]{i, 0});
20+
}
21+
for (int i = 0; i < n; i++) {
22+
pacific[0][i] = true;
23+
pacificQueue.offer(new int[]{0, i});
24+
}
25+
bfs(heights, pacificQueue, pacific);
26+
27+
Queue<int[]> atlanticQueue = new LinkedList<>();
28+
boolean[][] atlantic = new boolean[m][n];
29+
for (int i = 0; i < m; i++) {
30+
atlantic[i][n - 1] = true;
31+
atlanticQueue.offer(new int[]{i, n - 1});
32+
}
33+
for (int i = 0; i < n; i++) {
34+
atlantic[m - 1][i] = true;
35+
atlanticQueue.offer(new int[]{m - 1, i});
36+
}
37+
bfs(heights, atlanticQueue, atlantic);
38+
39+
List<List<Integer>> result = new ArrayList<>();
40+
for (int i = 0; i < m; i++) {
41+
for (int j = 0; j < n; j++) {
42+
if (pacific[i][j] && atlantic[i][j]) {
43+
result.add(List.of(i, j));
44+
}
45+
}
46+
}
47+
return result;
48+
}
49+
50+
51+
private void bfs(int[][] heights, Queue<int[]> queue, boolean[][] visit) {
52+
while (!queue.isEmpty()) {
53+
int[] curr = queue.poll();
54+
int cr = curr[0];
55+
int cc = curr[1];
56+
57+
for (int[] dir : directions) {
58+
int nr = cr + dir[0];
59+
int nc = cc + dir[1];
60+
61+
if (nr < 0 || nr > heights.length - 1 || nc < 0 || nc > heights[0].length - 1 || visit[nr][nc]) {
62+
continue;
63+
}
64+
if (heights[nr][nc] >= heights[cr][cc]) {
65+
visit[nr][nc] = true;
66+
queue.offer(new int[]{nr, nc});
67+
}
68+
}
69+
}
70+
}
71+
}

sum-of-two-integers/minji-go.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/sum-of-two-integers/">week9-4. sum-of-two-integers</a>
3+
* <li>Description: Given two integers a and b, return the sum of the two integers without using the operators + and -.</li>
4+
* <li>Topics: Math, Bit Manipulation </li>
5+
* <li>Time Complexity: O(1), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 40.51MB </li>
7+
*/
8+
class Solution {
9+
public int getSum(int a, int b) {
10+
while (b != 0) {
11+
int tmp = (a & b) << 1;
12+
a = (a ^ b);
13+
b = tmp;
14+
}
15+
return a;
16+
}
17+
}

word-search-ii/minji-go.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/word-search-ii/">week14-5. word-search-ii</a>
3+
* <li>Description: Given an m x n board of characters and a list of strings words, return all words on the board.</li>
4+
* <li>Topics: Array, String, Backtracking, Trie, Matrix</li>
5+
* <li>Time Complexity: O(M*N*4^L), Runtime 446ms </li>
6+
* <li>Space Complexity: O(K*L), Memory 44.81MB</li>
7+
* <li>Note: Refer to answer </li>
8+
*/
9+
class Solution {
10+
11+
class TrieNode {
12+
Map<Character, TrieNode> children = new HashMap<>();
13+
String word = null;
14+
}
15+
16+
public List<String> findWords(char[][] board, String[] words) {
17+
TrieNode root = buildTrie(words);
18+
List<String> result = new ArrayList<>();
19+
20+
for (int i = 0; i < board.length; i++) {
21+
for (int j = 0; j < board[0].length; j++) {
22+
if (root.children.containsKey(board[i][j])) {
23+
TrieNode node = root.children.get(board[i][j]);
24+
findWord(board, i, j, node, result);
25+
}
26+
}
27+
}
28+
29+
return result;
30+
}
31+
32+
private TrieNode buildTrie(String[] words) {
33+
TrieNode root = new TrieNode();
34+
35+
for (String word : words) {
36+
TrieNode node = root;
37+
for (char c : word.toCharArray()) {
38+
node = node.children.computeIfAbsent(c, k -> new TrieNode());
39+
}
40+
node.word = word;
41+
}
42+
return root;
43+
}
44+
45+
private int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
46+
47+
private void findWord(char[][] board, int r, int c, TrieNode node, List<String> result) {
48+
if (node.word != null) {
49+
result.add(node.word);
50+
node.word = null;
51+
}
52+
53+
char letter = board[r][c];
54+
board[r][c] = '#';
55+
56+
for (int[] direction : directions) {
57+
int nr = r + direction[0];
58+
int nc = c + direction[1];
59+
60+
if (nr < 0 || nr > board.length - 1 || nc < 0 || nc > board[0].length - 1) {
61+
continue;
62+
}
63+
if (node.children.containsKey(board[nr][nc])) {
64+
TrieNode nextNode = node.children.get(board[nr][nc]);
65+
findWord(board, nr, nc, nextNode, result);
66+
}
67+
}
68+
69+
board[r][c] = letter;
70+
}
71+
72+
}
73+

0 commit comments

Comments
 (0)