Skip to content

[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 1 commit into from
Jul 1, 2024
Merged
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
28 changes: 28 additions & 0 deletions combination-sum/bky373.java
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();
}
}
}
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;
}
}
94 changes: 94 additions & 0 deletions implement-trie-prefix-tree/bky373.java
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);
*/
22 changes: 22 additions & 0 deletions kth-smallest-element-in-a-bst/bky373.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배열을 이용하는 방법이 꽤나 깔끔하고 보기 좋네요!

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);
}
}
52 changes: 52 additions & 0 deletions word-search/bky373.java
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;
}
}