Skip to content

Commit 9832bbe

Browse files
week8 mission done
1 parent a944a26 commit 9832bbe

File tree

5 files changed

+338
-0
lines changed

5 files changed

+338
-0
lines changed

combination-sum/dev-jonghoonpark.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
- https://leetcode.com/problems/combination-sum/
2+
- time complexity : O(2^n)
3+
- space complexity : O(2^n)
4+
- https://algorithm.jonghoonpark.com/2024/06/23/leetcode-39
5+
6+
## bfs 로 풀기
7+
8+
```java
9+
class Solution {
10+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
11+
Arrays.sort(candidates);
12+
List<List<Integer>> result = new ArrayList<>();
13+
14+
Queue<Holder> queue = new LinkedList<>();
15+
queue.offer(new Holder(target));
16+
17+
while (!queue.isEmpty()) {
18+
Holder holder = queue.poll();
19+
int lastInput = !holder.combination.isEmpty() ? holder.combination.get(holder.combination.size() - 1) : 0;
20+
int left = holder.left;
21+
if (left == 0) {
22+
result.add(holder.combination);
23+
continue;
24+
}
25+
26+
for (int candidate : candidates) {
27+
if (candidate < lastInput) {
28+
continue;
29+
}
30+
31+
if (left - candidate >= 0) {
32+
queue.add(holder.next(candidate));
33+
} else {
34+
break;
35+
}
36+
}
37+
}
38+
39+
return result;
40+
}
41+
}
42+
43+
class Holder {
44+
int left;
45+
List<Integer> combination;
46+
47+
public Holder(int left) {
48+
this.left = left;
49+
this.combination = new ArrayList<>();
50+
}
51+
52+
private Holder(int left, List<Integer> combination) {
53+
this.left = left;
54+
this.combination = combination;
55+
}
56+
57+
public Holder next(int minus) {
58+
List<Integer> combinedList = new ArrayList<>(this.combination.size() + 1);
59+
combinedList.addAll(this.combination);
60+
combinedList.add(minus);
61+
return new Holder(this.left - minus, combinedList);
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return "{" +
67+
"left=" + left +
68+
", combination=" + combination +
69+
'}';
70+
}
71+
}
72+
```
73+
74+
## backtracking 으로 풀기
75+
76+
```java
77+
// TODO
78+
```
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
- https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
2+
- time complexity : O(n)
3+
- space complexity : O(n)
4+
- https://algorithm.jonghoonpark.com/2024/06/23/leetcode-105
5+
6+
```java
7+
class Solution {
8+
public TreeNode buildTree(int[] preorder, int[] inorder) {
9+
Map<Integer, Integer> inorderIndexMap = new HashMap<>();
10+
for (int i = 0; i < inorder.length; i++) {
11+
inorderIndexMap.put(inorder[i], i);
12+
}
13+
14+
return buildTree(inorderIndexMap, new Traversal(preorder), new Traversal(inorder));
15+
}
16+
17+
public TreeNode buildTree(Map<Integer, Integer> inorderIndexMap, Traversal preorderTraversal, Traversal inorderTraversal) {
18+
if(preorderTraversal.start > preorderTraversal.end) {
19+
return null;
20+
}
21+
22+
TreeNode treeNode = new TreeNode(preorderTraversal.getFirst());
23+
if(preorderTraversal.start == preorderTraversal.end) {
24+
return treeNode;
25+
}
26+
27+
int rootIndex = inorderIndexMap.get(preorderTraversal.getFirst());
28+
int leftSize = rootIndex - inorderTraversal.start;
29+
treeNode.left = buildTree(
30+
inorderIndexMap,
31+
preorderTraversal.subIterator(preorderTraversal.start + 1, preorderTraversal.start + leftSize),
32+
inorderTraversal.subIterator(inorderTraversal.start, rootIndex - 1)
33+
);
34+
treeNode.right = buildTree(
35+
inorderIndexMap,
36+
preorderTraversal.subIterator(preorderTraversal.start + leftSize + 1, preorderTraversal.end),
37+
inorderTraversal.subIterator(rootIndex + 1, inorderTraversal.end)
38+
);
39+
40+
return treeNode;
41+
}
42+
}
43+
44+
class TreeNode {
45+
int val;
46+
TreeNode left;
47+
TreeNode right;
48+
49+
TreeNode() {
50+
}
51+
52+
TreeNode(int val) {
53+
this.val = val;
54+
}
55+
56+
TreeNode(int val, TreeNode left, TreeNode right) {
57+
this.val = val;
58+
this.left = left;
59+
this.right = right;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "{" +
65+
"val=" + val +
66+
", left=" + left +
67+
", right=" + right +
68+
'}';
69+
}
70+
}
71+
72+
class Traversal {
73+
int[] array;
74+
int start;
75+
int end;
76+
77+
public Traversal(int[] array) {
78+
this.array = array;
79+
this.start = 0;
80+
this.end = array.length - 1;
81+
}
82+
83+
private Traversal(int[] array, int start, int end) {
84+
this.array = array;
85+
this.start = start;
86+
this.end = end;
87+
}
88+
89+
public Traversal subIterator(int start, int end) {
90+
return new Traversal(array, start, end);
91+
}
92+
93+
public int getFirst() {
94+
return array[start];
95+
}
96+
97+
public Traversal(int start, int end) {
98+
this.start = start;
99+
this.end = end;
100+
}
101+
102+
@Override
103+
public String toString() {
104+
return "{" +
105+
"start=" + start +
106+
", end=" + end +
107+
'}';
108+
}
109+
}
110+
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
- https://leetcode.com/problems/implement-trie-prefix-tree/
2+
- https://algorithm.jonghoonpark.com/2024/06/23/leetcode-208
3+
4+
## TC, SC
5+
6+
insert, search, startsWith 메소드의 경우 입력된 문자열의 길이를 n 이라 하였을 때 시간 복잡도는 `O(n)`이다. 공간 복잡도는 `insert된 문자열의 갯수``N` 이라 하고 `insert된 문자열의 길이의 평균``L`이라고 하였을 때 `O(N * L * 26)`이다. 26은 계수이기 때문에 생략할 수 있다.
7+
8+
## 풀이
9+
10+
```java
11+
class Trie {
12+
13+
Node root = new Node();
14+
15+
public Trie() {
16+
17+
}
18+
19+
public void insert(String word) {
20+
Node currentNode = root;
21+
for(char c : word.toCharArray()) {
22+
if(currentNode.nodes[c - 97] == null) {
23+
currentNode.nodes[c - 97] = new Node();
24+
}
25+
currentNode = currentNode.nodes[c - 97];
26+
}
27+
currentNode.val = word;
28+
}
29+
30+
public boolean search(String word) {
31+
Node currentNode = root;
32+
for(char c : word.toCharArray()) {
33+
if(currentNode.nodes[c - 97] == null) {
34+
return false;
35+
}
36+
currentNode = currentNode.nodes[c - 97];
37+
}
38+
39+
return currentNode.val != null && currentNode.val.equals(word);
40+
}
41+
42+
public boolean startsWith(String prefix) {
43+
Node currentNode = root;
44+
for(char c : prefix.toCharArray()) {
45+
if(currentNode.nodes[c - 97] == null) {
46+
return false;
47+
}
48+
currentNode = currentNode.nodes[c - 97];
49+
}
50+
return true;
51+
}
52+
}
53+
54+
class Node {
55+
String val;
56+
Node[] nodes = new Node[26];
57+
}
58+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
- https://leetcode.com/problems/kth-smallest-element-in-a-bst/
2+
- time complexity : O(n)
3+
- space complexity : O(n), 트리가 균등할 경우 O(logn)에 가까워진다.
4+
- https://algorithm.jonghoonpark.com/2024/06/23/leetcode-230
5+
6+
```java
7+
class Solution {
8+
public int kthSmallest(TreeNode root, int k) {
9+
return dfs(root, new Holder(k));
10+
}
11+
12+
public int dfs(TreeNode root, Holder holder) {
13+
if(root.left != null) {
14+
int left = dfs(root.left, holder);
15+
if (left != -1) {
16+
return left;
17+
}
18+
}
19+
holder.decrease();
20+
if (holder.k == 0) {
21+
return root.val;
22+
}
23+
if(root.right != null) {
24+
int right = dfs(root.right, holder);
25+
if (right != -1) {
26+
return right;
27+
}
28+
}
29+
return -1;
30+
}
31+
}
32+
33+
class Holder {
34+
int k;
35+
36+
public Holder(int k) {
37+
this.k = k;
38+
}
39+
40+
public void decrease() {
41+
this.k = this.k - 1;
42+
}
43+
}
44+
```

word-search/dev-jonghoonpark.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
- https://leetcode.com/problems/word-search/
2+
- https://algorithm.jonghoonpark.com/2024/06/23/leetcode-79
3+
4+
## TC, SC
5+
6+
`board` 의 길이를 `w`, `board[i]`의 길이를 `h`, 단어의 길이를 `n` 이라고 하였을 때.
7+
시간 복잡도는 `O(w * h * n)` 이다. 공간 복잡도는 `O(n)`이다.
8+
9+
## 풀이
10+
11+
```java
12+
class Solution {
13+
public boolean exist(char[][] board, String word) {
14+
char[] chars = word.toCharArray();
15+
for (int i = 0; i < board.length; i++) {
16+
for (int j = 0; j < board[0].length; j++) {
17+
if (dfs(board, chars, i, j, 0, word.length() - 1)) {
18+
return true;
19+
}
20+
}
21+
}
22+
23+
return false;
24+
}
25+
26+
public boolean dfs(char[][] board, char[] chars, int i, int j, int pointer, int end) {
27+
if (i < 0 || i > board.length - 1 || j < 0 || j > board[0].length - 1 || board[i][j] != chars[pointer]) {
28+
return false;
29+
}
30+
31+
if(pointer == end) {
32+
return true;
33+
}
34+
35+
int next = pointer + 1;
36+
char temp = board[i][j];
37+
board[i][j] = ' ';
38+
boolean result = dfs(board, chars, i + 1, j, next, end)
39+
|| dfs(board, chars, i - 1, j, next, end)
40+
|| dfs(board, chars, i, j + 1, next, end)
41+
|| dfs(board, chars, i, j - 1, next, end);
42+
if(!result) {
43+
board[i][j] = temp;
44+
}
45+
return result;
46+
}
47+
}
48+
```

0 commit comments

Comments
 (0)