-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8d181d0
feat: counting-bits
minji-go 6f1a70f
feat: binary-tree-level-order-traversal
minji-go 7f67f28
feat: house-robber-ii
minji-go 847ee56
feat: word-search-ii
minji-go c1930ef
feat: linked-list-cycle
minji-go 55ca2b8
feat: pacific-atlantic-water-flow
minji-go cddf0cf
feat: maximum-product-subarray
minji-go 9093505
feat: sum-of-two-integers
minji-go e2e1064
feat: minimum-window-substring
minji-go File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/maximum-product-subarray/">week9-3. maximum-product-subarray</a> | ||
* <li>Description: Given an integer array nums, find a subarray that has the largest product, and return the product. </li> | ||
* <li>Topics: Array, Dynamic Programming </li> | ||
* <li>Time Complexity: O(N), Runtime 2ms </li> | ||
* <li>Space Complexity: O(1), Memory 45.42MB </li> | ||
*/ | ||
class Solution { | ||
public int maxProduct(int[] nums) { | ||
int maxSoFar = nums[0]; | ||
int minSoFar = nums[0]; | ||
int result = nums[0]; | ||
|
||
for (int i = 1; i < nums.length; i++) { | ||
int cur = nums[i]; | ||
int tempMax = maxSoFar; | ||
|
||
maxSoFar = Math.max(cur, Math.max(cur * maxSoFar, cur * minSoFar)); | ||
minSoFar = Math.min(cur, Math.min(cur * tempMax, cur * minSoFar)); | ||
|
||
result = Math.max(result, maxSoFar); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/minimum-window-substring/">week9-5. minimum-window-substring</a> | ||
* <li>Description: return the minimum window substring of s such that every character in t (including duplicates) is included in the window </li> | ||
* <li>Topics: Hash Table, String, Sliding Window </li> | ||
* <li>Time Complexity: O(M+N), Runtime 23ms </li> | ||
* <li>Space Complexity: O(1), Memory 45.13MB </li> | ||
*/ | ||
class Solution { | ||
public String minWindow(String s, String t) { | ||
if (s.length() < t.length()) return ""; | ||
|
||
Map<Character, Integer> tmap = new HashMap<>(); | ||
for (char c : t.toCharArray()) { | ||
tmap.put(c, tmap.getOrDefault(c, 0) + 1); | ||
} | ||
|
||
Map<Character, Integer> smap = new HashMap<>(); | ||
int left = 0, right = 0; | ||
int tsize = tmap.size(); | ||
int ssize = 0; | ||
int start = -1, end = s.length(); | ||
|
||
while (right < s.length()) { | ||
char c = s.charAt(right++); | ||
if (tmap.containsKey(c)) { | ||
smap.put(c, smap.getOrDefault(c, 0) + 1); | ||
if (smap.get(c).intValue() == tmap.get(c).intValue()) { | ||
ssize++; | ||
} | ||
} | ||
|
||
while (ssize == tsize) { | ||
if (right - left < end - start) { | ||
start = left; | ||
end = right; | ||
} | ||
|
||
char l = s.charAt(left++); | ||
if (tmap.containsKey(l)) { | ||
smap.put(l, smap.get(l) - 1); | ||
if (smap.get(l).intValue() < tmap.get(l).intValue()) { | ||
ssize--; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return start == -1 ? "" : s.substring(start, end); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/sum-of-two-integers/">week9-4. sum-of-two-integers</a> | ||
* <li>Description: Given two integers a and b, return the sum of the two integers without using the operators + and -.</li> | ||
* <li>Topics: Math, Bit Manipulation </li> | ||
* <li>Time Complexity: O(1), Runtime 0ms </li> | ||
* <li>Space Complexity: O(1), Memory 40.51MB </li> | ||
*/ | ||
class Solution { | ||
public int getSum(int a, int b) { | ||
while (b != 0) { | ||
int tmp = (a & b) << 1; | ||
a = (a ^ b); | ||
b = tmp; | ||
} | ||
return a; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아래 코드라면 offset * 2 연산을 1번으로 줄여 성능 개선이 조금이라도 이뤄질 수 있을 것 같습니다!