Skip to content

Commit 1720f29

Browse files
Merge pull request #156 from dev-jonghoonpark/main
[๋ฐ•์ข…ํ›ˆ] 9์ฃผ์ฐจ ๋‹ต์•ˆ ์ œ์ถœ
2 parents 30dd3b7 + fad9b25 commit 1720f29

File tree

5 files changed

+397
-0
lines changed

5 files changed

+397
-0
lines changed

โ€Žclone-graph/dev-jonghoonpark.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/clone-graph/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/02/13/leetcode-133
3+
4+
```java
5+
class Solution {
6+
public Node cloneGraph(Node node) {
7+
return cloneGraph(new HashMap<>(), node);
8+
}
9+
10+
private Node cloneGraph(Map<Integer, Node> map, Node node) {
11+
if(node == null) {
12+
return null;
13+
}
14+
15+
if (map.containsKey(node.val)) {
16+
return map.get(node.val);
17+
}
18+
19+
Node copy = new Node(node.val);
20+
map.put(node.val, copy);
21+
22+
for (int i = 0; i < node.neighbors.size(); i++) {
23+
Node neighborNode = node.neighbors.get(i);
24+
copy.neighbors.add(map.getOrDefault(neighborNode.val, cloneGraph(map, node.neighbors.get(i))));
25+
}
26+
27+
return copy;
28+
}
29+
}
30+
```
31+
32+
### TC, SC
33+
34+
node(vertex)์˜ ์ˆ˜๋ฅผ `V`, edge์˜ ์ˆ˜๋ฅผ `E` ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ๊ฐ ๋…ธ๋“œ ๋งˆ๋‹ค edge์˜ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต์„ ํ•ด์•ผํ•œ๋‹ค.
35+
์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” `O(V + E)` ์ด๋‹ค. ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” `O(V)`์ด๋‹ค.
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/course-schedule/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/03/01/leetcode-207
3+
4+
```java
5+
public class Solution {
6+
public boolean canFinish(int numCourses, int[][] prerequisites) {
7+
if (prerequisites.length == 0) {
8+
return true;
9+
}
10+
11+
Map<Integer, Node> vertexMap = new HashMap<>();
12+
13+
for (int[] prerequisite : prerequisites) {
14+
vertexMap.putIfAbsent(prerequisite[0], new Node(prerequisite[0]));
15+
vertexMap.putIfAbsent(prerequisite[1], new Node(prerequisite[1]));
16+
17+
Node vertex1 = vertexMap.get(prerequisite[0]);
18+
Node vertex2 = vertexMap.get(prerequisite[1]);
19+
20+
vertex1.edges.add(vertex2);
21+
vertex2.reversedEdges.add(vertex1);
22+
}
23+
24+
Deque<Integer> deque = new LinkedList<>();
25+
26+
int[] degrees = new int[numCourses];
27+
for (int i = 0; i < degrees.length; i++) {
28+
Node vertex = vertexMap.get(i);
29+
if (vertex != null) {
30+
degrees[i] = vertex.edges.size();
31+
if (degrees[i] == 0) {
32+
deque.addLast(i);
33+
}
34+
}
35+
}
36+
37+
while(!deque.isEmpty()) {
38+
int vertexId = deque.removeFirst();
39+
Node vertex = vertexMap.get(vertexId);
40+
for (Node node : vertex.reversedEdges) {
41+
degrees[node.id]--;
42+
if (degrees[node.id] == 0) {
43+
deque.addLast(node.id);
44+
}
45+
}
46+
vertexMap.remove(vertexId);
47+
}
48+
49+
return vertexMap.isEmpty();
50+
}
51+
}
52+
53+
class Node {
54+
int id;
55+
List<Node> edges;
56+
List<Node> reversedEdges;
57+
58+
public Node(int id) {
59+
this.id = id;
60+
edges = new ArrayList<>();
61+
reversedEdges = new ArrayList<>();
62+
}
63+
}
64+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
- https://leetcode.com/problems/design-add-and-search-words-data-structure
2+
- https://algorithm.jonghoonpark.com/2024/06/30/leetcode-211
3+
4+
## brute force
5+
6+
์•„์Šฌ์•„์Šฌํ•˜๊ฒŒ ํ†ต๊ณผํ•œ๋‹ค.
7+
8+
```java
9+
class WordDictionary {
10+
11+
Set<String> wordSet;
12+
13+
public WordDictionary() {
14+
wordSet = new HashSet<>();
15+
}
16+
17+
public void addWord(String word) {
18+
wordSet.add(word);
19+
}
20+
21+
public boolean search(String word) {
22+
Deque<String> queue = new ArrayDeque<>();
23+
queue.push(word);
24+
25+
while (queue.getFirst().contains(".")) {
26+
String _word = queue.removeFirst();
27+
String pre = _word.substring(0, _word.indexOf("."));
28+
String post = _word.substring(_word.indexOf(".") + 1);
29+
30+
for (char c = 'a'; c <= 'z'; c++) {
31+
queue.addLast(pre + c + post);
32+
}
33+
}
34+
35+
while (!queue.isEmpty()) {
36+
String _word = queue.removeFirst();
37+
if (wordSet.contains(_word)) {
38+
return true;
39+
}
40+
}
41+
42+
return false;
43+
}
44+
}
45+
```
46+
47+
### TC, SC
48+
49+
- `.` ์ด ์—†์„ ๋•Œ
50+
- ์‹œ๊ฐ„ ๋ณต์žก๋„ : `O(1)`
51+
- ๊ณต๊ฐ„ ๋ณต์žก๋„ : `O(1)`
52+
- `.` ์ด ์žˆ์„ ๋•Œ
53+
- ์‹œ๊ฐ„ ๋ณต์žก๋„ : `O(26^N)`
54+
- ๊ณต๊ฐ„ ๋ณต์žก๋„ : `O(26^N)`
55+
- ์—ฌ๊ธฐ์„œ N์€ `.` ์˜ ์ˆ˜
56+
57+
## trie
58+
59+
[208. Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) ๋ฌธ์ œ ์—์„œ ์‚ฌ์šฉํ•œ Trie ์žฌ์‚ฌ์šฉ.
60+
61+
```java
62+
class WordDictionary {
63+
64+
Trie trie; // Trie ๊ตฌํ˜„์€ ์ƒ๋žต
65+
66+
public WordDictionary() {
67+
trie = new Trie();
68+
}
69+
70+
public void addWord(String word) {
71+
trie.insert(word);
72+
}
73+
74+
public boolean search(String word) {
75+
if (word.contains(".")) {
76+
String pre = word.substring(0, word.indexOf("."));
77+
String post = word.substring(word.indexOf(".") + 1);
78+
79+
if (trie.startsWith(pre)) {
80+
for (char c = 'a'; c <= 'z'; c++) {
81+
if (search(pre + c + post)) {
82+
return true;
83+
}
84+
}
85+
}
86+
87+
return false;
88+
}
89+
90+
return trie.search(word);
91+
}
92+
93+
94+
}
95+
```
96+
97+
### TC, SC
98+
99+
์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ `L`, `.` ์˜ ์ˆ˜๋ฅผ `N` ์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ
100+
101+
addWord ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” `O(L)`์ด๋‹ค.
102+
search ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ n ์ด๋ผ ํ•˜์˜€์„ ๋•Œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” `O(L * 26 ^ N)`์ด๋‹ค.
103+
104+
๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” Trie ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉ๋œ ๊ณต๊ฐ„์ด๋‹ค. `insert๋œ ๋ฌธ์ž์—ด ๊ธธ์ด์˜ ํ‰๊ท ` ๋ฅผ `avg(L)`์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ `O(avg(L) * 26)`์ด๋‹ค. 26์€ ๊ณ„์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
105+
106+
## trie ๊ฐœ์„ 
107+
108+
์ด ๋ฌธ์ œ์— ์ ํ•ฉํ•˜๋„๋ก search๋ฅผ ์ˆ˜์ •ํ•˜์˜€๋‹ค.
109+
110+
```java
111+
class WordDictionary {
112+
113+
Trie trie;
114+
115+
public WordDictionary() {
116+
trie = new Trie();
117+
}
118+
119+
public void addWord(String word) {
120+
trie.insert(word);
121+
}
122+
123+
public boolean search(String word) {
124+
return trie.search(word);
125+
}
126+
127+
128+
}
129+
130+
class Trie {
131+
132+
Node root = new Node();
133+
134+
public Trie() {
135+
136+
}
137+
138+
public void insert(String word) {
139+
Node currentNode = root;
140+
for (char c : word.toCharArray()) {
141+
if (currentNode.nodes[c - 97] == null) {
142+
currentNode.nodes[c - 97] = new Node();
143+
}
144+
currentNode = currentNode.nodes[c - 97];
145+
}
146+
currentNode.val = word;
147+
}
148+
149+
public boolean search(String word) {
150+
return search(root, word, 0);
151+
}
152+
153+
public boolean search(Node node, String word, int index) {
154+
if (node == null) {
155+
return false;
156+
}
157+
158+
if (node.val != null && node.val.length() == word.length()) {
159+
return true;
160+
}
161+
162+
if (index >= word.length()) {
163+
return false;
164+
}
165+
166+
char c = word.charAt(index);
167+
168+
if (c == '.') {
169+
for (char _c = 'a'; _c <= 'z'; _c++) {
170+
if (search(node.nodes[_c - 97], word, index + 1)) {
171+
return true;
172+
}
173+
}
174+
return false;
175+
} else if (node.nodes[c - 97] == null) {
176+
return false;
177+
}
178+
179+
return search(node.nodes[c - 97], word, index + 1);
180+
}
181+
}
182+
183+
class Node {
184+
String val;
185+
Node[] nodes = new Node[26];
186+
}
187+
```
188+
189+
### TC, SC
190+
191+
์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ `L`, `.` ์˜ ์ˆ˜๋ฅผ `N` ์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ
192+
193+
addWord ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” `O(L)`์ด๋‹ค.
194+
search ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ n ์ด๋ผ ํ•˜์˜€์„ ๋•Œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” `O(L * 26 ^ N)`์ด๋‹ค.
195+
๊ฐœ์„  ์ „๊ณผ ๋น„๊ตํ•ด๋ดค์„ ๋•Œ ํ‘œ๊ธฐ์ƒ์œผ๋กœ๋Š” ์ฐจ์ด๊ฐ€ ์—†์œผ๋‚˜, ๋ถˆํ•„์š”ํ•œ ๊ณผ์ •์„ ์ œ๊ฑฐํ•˜๊ฒŒ๋˜์–ด์„œ ์‹œ๊ฐ„์ด ๋งค์šฐ ๋‹จ์ถ•๋œ๋‹ค.
196+
(`trie.startsWith(pre)`์ด ์‚ฌ๋ผ์กŒ๊ณ , search์˜ ํ˜ธ์ถœ ํšŸ์ˆ˜๊ฐ€ ์ค„์–ด๋“ฌ.)
197+
198+
๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” Trie ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉ๋œ ๊ณต๊ฐ„์ด๋‹ค. `insert๋œ ๋ฌธ์ž์—ด ๊ธธ์ด์˜ ํ‰๊ท ` ๋ฅผ `avg(L)`์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ `O(avg(L) * 26)`์ด๋‹ค. 26์€ ๊ณ„์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/number-of-islands/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/03/31/leetcode-200
3+
4+
```java
5+
class Solution {
6+
public int numIslands(char[][] grid) {
7+
int w = grid.length;
8+
int h = grid[0].length;
9+
10+
int count = 0;
11+
for (int i = 0; i < w; i++) {
12+
for (int j = 0; j < h; j++) {
13+
if (grid[i][j] == '1') {
14+
dfs(grid, i,j);
15+
count++;
16+
}
17+
}
18+
}
19+
return count;
20+
}
21+
22+
public void dfs(char[][] grid, int i, int j) {
23+
if(i < 0 || i >= w || j < 0 || j >= h || grid[i][j] == '0') {
24+
return;
25+
}
26+
27+
grid[i][j] = '0';
28+
29+
dfs(grid, i-1, j);
30+
dfs(grid, i, j-1);
31+
dfs(grid, i+1, j);
32+
dfs(grid, i, j+1);
33+
}
34+
}
35+
```
36+
37+
## TC, SC
38+
39+
์ฝ”๋“œ์— ์ •์˜ํ•œ ๋Œ€๋กœ grid์˜ ๊ธธ์ด๋ฅผ `w`, grid[0]์˜ ๊ธธ์ด๋ฅผ `h`๋กœ ์ •์˜ํ–ˆ์„ ๋•Œ,
40+
์ด ์ฝ”๋“œ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(w \* h), ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(w \* h) ์ด๋‹ค.

0 commit comments

Comments
ย (0)