Skip to content

Commit 6d99530

Browse files
authored
Solve 8th week problems (DaleStudy#161)
1 parent c4443ba commit 6d99530

File tree

5 files changed

+227
-0
lines changed

5 files changed

+227
-0
lines changed

combination-sum/bky373.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* time: O(n^2)
3+
* space: O(n)
4+
*/
5+
class Solution {
6+
7+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
8+
List<List<Integer>> result = new ArrayList<>();
9+
List<Integer> combination = new LinkedList<>();
10+
backtrack(candidates, target, combination, 0, result);
11+
return result;
12+
}
13+
14+
void backtrack(int[] candidates, int remain, List<Integer> combination, int startIndex, List<List<Integer>> result) {
15+
if (remain == 0) {
16+
result.add(new ArrayList<>(combination));
17+
return;
18+
} else if (remain < 0) {
19+
return;
20+
}
21+
22+
for (int i = startIndex; i < candidates.length; i++) {
23+
combination.add(candidates[i]);
24+
backtrack(candidates, remain - candidates[i], combination, i, result);
25+
combination.removeLast();
26+
}
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* time: O(N)
3+
* space: O(N)
4+
*/
5+
class Solution {
6+
7+
int preorderIndex;
8+
Map<Integer, Integer> inorderIndexMap;
9+
10+
public TreeNode buildTree(int[] preorder, int[] inorder) {
11+
preorderIndex = 0;
12+
inorderIndexMap = new HashMap<>();
13+
for (int i = 0; i < inorder.length; i++) {
14+
inorderIndexMap.put(inorder[i], i);
15+
}
16+
return arrayToTree(preorder, 0, preorder.length - 1);
17+
}
18+
19+
private TreeNode arrayToTree(int[] preorder, int left, int right) {
20+
if (left > right) {
21+
return null;
22+
}
23+
24+
int rootValue = preorder[preorderIndex++];
25+
TreeNode root = new TreeNode(rootValue);
26+
27+
root.left = arrayToTree(preorder, left, inorderIndexMap.get(rootValue) - 1);
28+
root.right = arrayToTree(preorder, inorderIndexMap.get(rootValue) + 1, right);
29+
return root;
30+
}
31+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* time: O(M)
3+
* space: O(N*M)
4+
*/
5+
public class TrieNode {
6+
private static final int MAX_SIZE = 26;
7+
8+
public TrieNode[] subNodes;
9+
public boolean isEndOfWord;
10+
11+
public TrieNode() {
12+
subNodes = new TrieNode[MAX_SIZE];
13+
}
14+
15+
public boolean contains(char ch) {
16+
return subNodes[ch - 'a'] != null;
17+
}
18+
19+
public TrieNode getSubNode(char ch) {
20+
return subNodes[ch - 'a'];
21+
}
22+
23+
public void put(char ch, TrieNode node) {
24+
subNodes[ch - 'a'] = node;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "TrieNode{sub=" + Arrays.toString(subNodes) + "}";
30+
}
31+
}
32+
33+
34+
class Trie {
35+
36+
public TrieNode root;
37+
38+
public Trie() {
39+
root = new TrieNode();
40+
}
41+
42+
public void insert(String word) {
43+
TrieNode node = root;
44+
for (int i = 0; i < word.length(); i++) {
45+
char ch = word.charAt(i);
46+
if (!node.contains(ch)) {
47+
node.put(ch, new TrieNode());
48+
}
49+
node = node.getSubNode(ch);
50+
}
51+
node.isEndOfWord = true;
52+
}
53+
54+
public boolean search(String word) {
55+
TrieNode node = root;
56+
for (int i = 0; i < word.length(); i++) {
57+
if (node == null) {
58+
return false;
59+
}
60+
if (!node.contains(word.charAt(i))) {
61+
return false;
62+
}
63+
node = node.getSubNode(word.charAt(i));
64+
if (i == word.length() - 1 && node.isEndOfWord) {
65+
return true;
66+
}
67+
}
68+
return false;
69+
}
70+
71+
public boolean startsWith(String prefix) {
72+
TrieNode node = root;
73+
for (int i = 0; i < prefix.length(); i++) {
74+
if (node == null) {
75+
return false;
76+
}
77+
if (!node.contains(prefix.charAt(i))) {
78+
return false;
79+
}
80+
node = node.getSubNode(prefix.charAt(i));
81+
}
82+
return true;
83+
}
84+
85+
86+
}
87+
88+
/**
89+
* Your Trie object will be instantiated and called as such:
90+
* Trie obj = new Trie();
91+
* obj.insert(word);
92+
* boolean param_2 = obj.search(word);
93+
* boolean param_3 = obj.startsWith(prefix);
94+
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* time: O(N)
3+
* space: O(N)
4+
*/
5+
class Solution {
6+
7+
List<Integer> inorderList = new ArrayList<>();
8+
9+
public int kthSmallest(TreeNode root, int k) {
10+
inorder(root);
11+
return inorderList.get(k - 1);
12+
}
13+
14+
public void inorder(TreeNode cur) {
15+
if (cur == null) {
16+
return;
17+
}
18+
inorder(cur.left);
19+
inorderList.add(cur.val);
20+
inorder(cur.right);
21+
}
22+
}

word-search/bky373.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* time: O(N * 4^L)
3+
* - N: the number of cells in the board
4+
* - L: the length of the word
5+
* - 4: the length of directions
6+
* space: O(L)
7+
* - L: the recursive backtracking function call stacks.
8+
*/
9+
class Solution {
10+
11+
private char[][] board;
12+
private int rLen;
13+
private int cLen;
14+
15+
public boolean exist(char[][] board, String word) {
16+
this.board = board;
17+
this.rLen = board.length;
18+
this.cLen = board[0].length;
19+
20+
for (int row = 0; row < this.rLen; ++row) {
21+
for (int col = 0; col < this.cLen; ++col) {
22+
if (this.backtrack(row, col, word, 0)) {
23+
return true;
24+
}
25+
}
26+
}
27+
return false;
28+
}
29+
30+
private boolean backtrack(int row, int col, String word, int matchIndex) {
31+
if (matchIndex >= word.length()) {
32+
return true;
33+
}
34+
35+
if (row < 0 || row == this.rLen || col < 0 || col == this.cLen || this.board[row][col] != word.charAt(matchIndex)) {
36+
return false;
37+
}
38+
39+
this.board[row][col] = ' ';
40+
41+
int[] rowDirs = {0, 1, 0, -1};
42+
int[] colDirs = {1, 0, -1, 0};
43+
for (int dir = 0; dir < 4; ++dir) {
44+
if (backtrack(row + rowDirs[dir], col + colDirs[dir], word, matchIndex + 1)) {
45+
return true;
46+
}
47+
}
48+
49+
this.board[row][col] = word.charAt(matchIndex);
50+
return false;
51+
}
52+
}

0 commit comments

Comments
 (0)