Skip to content

[minji-go] week 14 solutions #1633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions binary-tree-level-order-traversal/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* <a href="https://leetcode.com/problems/binary-tree-level-order-traversal/">week14-2. binary-tree-level-order-traversal</a>
* <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>
* <li>Topics: Tree, Breadth-First Search, Binary Tree</li>
* <li>Time Complexity: O(N), Runtime 1ms </li>
* <li>Space Complexity: O(N), Memory 45.3MB </li>
*/

class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null) {
return Collections.emptyList();
}

Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);

List<List<Integer>> results = new ArrayList<>();
while (!queue.isEmpty()) {
List<Integer> result = new ArrayList<>();

int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
result.add(node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
results.add(result);
}

return results;
}
}
20 changes: 20 additions & 0 deletions counting-bits/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* <a href="https://leetcode.com/problems/counting-bits/">week14-1. counting-bits</a>
* <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>
* <li>Topics: Dynamic Programming, Bit Manipulation</li>
* <li>Time Complexity: O(N), Runtime 2ms </li>
* <li>Space Complexity: O(N), Memory 46.64MB </li>
*/
class Solution {
public int[] countBits(int n) {
int[] count = new int[n + 1];

int offset = 1;
for (int i = 1; i <= n; i++) {
if (i == offset * 2) offset *= 2;
count[i] = count[i - offset] + 1;
}

return count;
}
}
27 changes: 27 additions & 0 deletions house-robber-ii/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* <a href="https://leetcode.com/problems/house-robber-ii/">week14-3. house-robber-ii</a>
* <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>
* <li>Topics: Array, Dynamic Programming </li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(1), Memory 40.6MB</li>
*/

class Solution {
public int rob(int[] nums) {
int n = nums.length;
if (n == 1) return nums[0];
if (n == 2) return Math.max(nums[0], nums[1]);

return Math.max(rob(nums, 0, n - 2), rob(nums, 1, n - 1));
}

public int rob(int[] nums, int start, int end) {
int prev1 = 0, prev2 = 0;
for (int i = start; i <= end; i++) {
int temp = Math.max(prev2, prev1 + nums[i]);
prev1 = prev2;
prev2 = temp;
}
return prev2;
}
}
27 changes: 27 additions & 0 deletions linked-list-cycle/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* <a href="https://leetcode.com/problems/linked-list-cycle/">week9-1. linked-list-cycle</a>
* <li>Description: Return true if there is a cycle in the linked list. </li>
* <li>Topics: Hash Table, Linked List, Two Pointers</li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(1), Memory 44.37MB</li>
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}

ListNode slow = head;
ListNode fast = head.next;

while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}

return true;
}
}
71 changes: 71 additions & 0 deletions pacific-atlantic-water-flow/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* <a href="https://leetcode.com/problems/pacific-atlantic-water-flow/">week9-2. pacific-atlantic-water-flow</a>
* <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>
* <li>Topics: Array, Depth-First Search, Breadth-First Search, Matrix</li>
* <li>Time Complexity: O(MN), Runtime 9ms </li>
* <li>Space Complexity: O(MN), Memory 45.18MB</li>
*/
class Solution {
private int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

public List<List<Integer>> pacificAtlantic(int[][] heights) {
int m = heights.length;
int n = heights[0].length;

Queue<int[]> pacificQueue = new LinkedList<>();
boolean[][] pacific = new boolean[m][n];
for (int i = 0; i < m; i++) {
pacific[i][0] = true;
pacificQueue.offer(new int[]{i, 0});
}
for (int i = 0; i < n; i++) {
pacific[0][i] = true;
pacificQueue.offer(new int[]{0, i});
}
bfs(heights, pacificQueue, pacific);

Queue<int[]> atlanticQueue = new LinkedList<>();
boolean[][] atlantic = new boolean[m][n];
for (int i = 0; i < m; i++) {
atlantic[i][n - 1] = true;
atlanticQueue.offer(new int[]{i, n - 1});
}
for (int i = 0; i < n; i++) {
atlantic[m - 1][i] = true;
atlanticQueue.offer(new int[]{m - 1, i});
}
bfs(heights, atlanticQueue, atlantic);

List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (pacific[i][j] && atlantic[i][j]) {
result.add(List.of(i, j));
}
}
}
return result;
}


private void bfs(int[][] heights, Queue<int[]> queue, boolean[][] visit) {
while (!queue.isEmpty()) {
int[] curr = queue.poll();
int cr = curr[0];
int cc = curr[1];

for (int[] dir : directions) {
int nr = cr + dir[0];
int nc = cc + dir[1];

if (nr < 0 || nr > heights.length - 1 || nc < 0 || nc > heights[0].length - 1 || visit[nr][nc]) {
continue;
}
if (heights[nr][nc] >= heights[cr][cc]) {
visit[nr][nc] = true;
queue.offer(new int[]{nr, nc});
}
}
}
}
}
73 changes: 73 additions & 0 deletions word-search-ii/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* <a href="https://leetcode.com/problems/word-search-ii/">week14-5. word-search-ii</a>
* <li>Description: Given an m x n board of characters and a list of strings words, return all words on the board.</li>
* <li>Topics: Array, String, Backtracking, Trie, Matrix</li>
* <li>Time Complexity: O(M*N*4^L), Runtime 446ms </li>
* <li>Space Complexity: O(K*L), Memory 44.81MB</li>
* <li>Note: Refer to answer </li>
*/
class Solution {

class TrieNode {
Map<Character, TrieNode> children = new HashMap<>();
String word = null;
}

public List<String> findWords(char[][] board, String[] words) {
TrieNode root = buildTrie(words);
List<String> result = new ArrayList<>();

for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (root.children.containsKey(board[i][j])) {
TrieNode node = root.children.get(board[i][j]);
findWord(board, i, j, node, result);
}
}
}

return result;
}

private TrieNode buildTrie(String[] words) {
TrieNode root = new TrieNode();

for (String word : words) {
TrieNode node = root;
for (char c : word.toCharArray()) {
node = node.children.computeIfAbsent(c, k -> new TrieNode());
}
node.word = word;
}
return root;
}

private int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

private void findWord(char[][] board, int r, int c, TrieNode node, List<String> result) {
if (node.word != null) {
result.add(node.word);
node.word = null;
}

char letter = board[r][c];
board[r][c] = '#';

for (int[] direction : directions) {
int nr = r + direction[0];
int nc = c + direction[1];

if (nr < 0 || nr > board.length - 1 || nc < 0 || nc > board[0].length - 1) {
continue;
}
if (node.children.containsKey(board[nr][nc])) {
TrieNode nextNode = node.children.get(board[nr][nc]);
findWord(board, nr, nc, nextNode, result);
}
}

board[r][c] = letter;
}

}