-
-
Notifications
You must be signed in to change notification settings - Fork 195
[bky373] 8th Week Solutions #161
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
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,28 @@ | ||
/* | ||
* time: O(n^2) | ||
* space: O(n) | ||
*/ | ||
class Solution { | ||
|
||
public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
List<List<Integer>> result = new ArrayList<>(); | ||
List<Integer> combination = new LinkedList<>(); | ||
backtrack(candidates, target, combination, 0, result); | ||
return result; | ||
} | ||
|
||
void backtrack(int[] candidates, int remain, List<Integer> combination, int startIndex, List<List<Integer>> result) { | ||
if (remain == 0) { | ||
result.add(new ArrayList<>(combination)); | ||
return; | ||
} else if (remain < 0) { | ||
return; | ||
} | ||
|
||
for (int i = startIndex; i < candidates.length; i++) { | ||
combination.add(candidates[i]); | ||
backtrack(candidates, remain - candidates[i], combination, i, result); | ||
combination.removeLast(); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
construct-binary-tree-from-preorder-and-inorder-traversal/bky373.java
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,31 @@ | ||
/* | ||
* time: O(N) | ||
* space: O(N) | ||
*/ | ||
class Solution { | ||
|
||
int preorderIndex; | ||
Map<Integer, Integer> inorderIndexMap; | ||
|
||
public TreeNode buildTree(int[] preorder, int[] inorder) { | ||
preorderIndex = 0; | ||
inorderIndexMap = new HashMap<>(); | ||
for (int i = 0; i < inorder.length; i++) { | ||
inorderIndexMap.put(inorder[i], i); | ||
} | ||
return arrayToTree(preorder, 0, preorder.length - 1); | ||
} | ||
|
||
private TreeNode arrayToTree(int[] preorder, int left, int right) { | ||
if (left > right) { | ||
return null; | ||
} | ||
|
||
int rootValue = preorder[preorderIndex++]; | ||
TreeNode root = new TreeNode(rootValue); | ||
|
||
root.left = arrayToTree(preorder, left, inorderIndexMap.get(rootValue) - 1); | ||
root.right = arrayToTree(preorder, inorderIndexMap.get(rootValue) + 1, right); | ||
return root; | ||
} | ||
} |
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,94 @@ | ||
/* | ||
* time: O(M) | ||
* space: O(N*M) | ||
*/ | ||
public class TrieNode { | ||
private static final int MAX_SIZE = 26; | ||
|
||
public TrieNode[] subNodes; | ||
public boolean isEndOfWord; | ||
|
||
public TrieNode() { | ||
subNodes = new TrieNode[MAX_SIZE]; | ||
} | ||
|
||
public boolean contains(char ch) { | ||
return subNodes[ch - 'a'] != null; | ||
} | ||
|
||
public TrieNode getSubNode(char ch) { | ||
return subNodes[ch - 'a']; | ||
} | ||
|
||
public void put(char ch, TrieNode node) { | ||
subNodes[ch - 'a'] = node; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TrieNode{sub=" + Arrays.toString(subNodes) + "}"; | ||
} | ||
} | ||
|
||
|
||
class Trie { | ||
|
||
public TrieNode root; | ||
|
||
public Trie() { | ||
root = new TrieNode(); | ||
} | ||
|
||
public void insert(String word) { | ||
TrieNode node = root; | ||
for (int i = 0; i < word.length(); i++) { | ||
char ch = word.charAt(i); | ||
if (!node.contains(ch)) { | ||
node.put(ch, new TrieNode()); | ||
} | ||
node = node.getSubNode(ch); | ||
} | ||
node.isEndOfWord = true; | ||
} | ||
|
||
public boolean search(String word) { | ||
TrieNode node = root; | ||
for (int i = 0; i < word.length(); i++) { | ||
if (node == null) { | ||
return false; | ||
} | ||
if (!node.contains(word.charAt(i))) { | ||
return false; | ||
} | ||
node = node.getSubNode(word.charAt(i)); | ||
if (i == word.length() - 1 && node.isEndOfWord) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public boolean startsWith(String prefix) { | ||
TrieNode node = root; | ||
for (int i = 0; i < prefix.length(); i++) { | ||
if (node == null) { | ||
return false; | ||
} | ||
if (!node.contains(prefix.charAt(i))) { | ||
return false; | ||
} | ||
node = node.getSubNode(prefix.charAt(i)); | ||
} | ||
return true; | ||
} | ||
|
||
|
||
} | ||
|
||
/** | ||
* Your Trie object will be instantiated and called as such: | ||
* Trie obj = new Trie(); | ||
* obj.insert(word); | ||
* boolean param_2 = obj.search(word); | ||
* boolean param_3 = obj.startsWith(prefix); | ||
*/ |
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,22 @@ | ||
/* | ||
* time: O(N) | ||
* space: O(N) | ||
*/ | ||
class Solution { | ||
|
||
List<Integer> inorderList = new ArrayList<>(); | ||
|
||
public int kthSmallest(TreeNode root, int k) { | ||
inorder(root); | ||
return inorderList.get(k - 1); | ||
} | ||
|
||
public void inorder(TreeNode cur) { | ||
if (cur == null) { | ||
return; | ||
} | ||
inorder(cur.left); | ||
inorderList.add(cur.val); | ||
inorder(cur.right); | ||
} | ||
} |
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,52 @@ | ||
/* | ||
* time: O(N * 4^L) | ||
* - N: the number of cells in the board | ||
* - L: the length of the word | ||
* - 4: the length of directions | ||
* space: O(L) | ||
* - L: the recursive backtracking function call stacks. | ||
*/ | ||
class Solution { | ||
|
||
private char[][] board; | ||
private int rLen; | ||
private int cLen; | ||
|
||
public boolean exist(char[][] board, String word) { | ||
this.board = board; | ||
this.rLen = board.length; | ||
this.cLen = board[0].length; | ||
|
||
for (int row = 0; row < this.rLen; ++row) { | ||
for (int col = 0; col < this.cLen; ++col) { | ||
if (this.backtrack(row, col, word, 0)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean backtrack(int row, int col, String word, int matchIndex) { | ||
if (matchIndex >= word.length()) { | ||
return true; | ||
} | ||
|
||
if (row < 0 || row == this.rLen || col < 0 || col == this.cLen || this.board[row][col] != word.charAt(matchIndex)) { | ||
return false; | ||
} | ||
|
||
this.board[row][col] = ' '; | ||
|
||
int[] rowDirs = {0, 1, 0, -1}; | ||
int[] colDirs = {1, 0, -1, 0}; | ||
for (int dir = 0; dir < 4; ++dir) { | ||
if (backtrack(row + rowDirs[dir], col + colDirs[dir], word, matchIndex + 1)) { | ||
return true; | ||
} | ||
} | ||
|
||
this.board[row][col] = word.charAt(matchIndex); | ||
return false; | ||
} | ||
} |
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.
배열을 이용하는 방법이 꽤나 깔끔하고 보기 좋네요!