Skip to content

Commit d474ce1

Browse files
authored
Merge pull request #2525 from sangbeenmoon/main
[sangbeenmoon] WEEK 06 Solutions
2 parents ccef8de + f246990 commit d474ce1

3 files changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class WordDictionary {
5+
6+
7+
Map<Character, WordDictionary> map;
8+
boolean isEnd = false;
9+
10+
public WordDictionary() {
11+
map = new HashMap<>();
12+
}
13+
14+
public void addWord(String word) {
15+
char ch = word.charAt(0);
16+
WordDictionary node;
17+
if (map.containsKey(ch)) {
18+
node = map.get(ch);
19+
} else {
20+
node = new WordDictionary();
21+
}
22+
23+
map.put(ch, node);
24+
25+
if (word.length() == 1) {
26+
node.isEnd = true;
27+
return;
28+
}
29+
30+
node.addWord(word.substring(1));
31+
}
32+
33+
public boolean search(String word) {
34+
if (word.length() == 0) {
35+
return isEnd;
36+
}
37+
38+
char ch = word.charAt(0);
39+
WordDictionary node;
40+
41+
if (ch == '.') {
42+
for (Map.Entry<Character, WordDictionary> entry : map.entrySet()) {
43+
if (entry.getValue().search(word.substring(1))){
44+
return true;
45+
}
46+
}
47+
}
48+
49+
if (map.containsKey(ch)) {
50+
node = map.get(ch);
51+
return node.search(word.substring(1));
52+
}
53+
54+
return false;
55+
}
56+
}
57+
58+
/**
59+
* Your WordDictionary object will be instantiated and called as such:
60+
* WordDictionary obj = new WordDictionary();
61+
* obj.addWord(word);
62+
* boolean param_2 = obj.search(word);
63+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# hint 를 보고 해결.
2+
# DP 로도 풀 수 있음.
3+
4+
class Solution:
5+
def lengthOfLIS(self, nums: List[int]) -> int:
6+
seq = []
7+
8+
9+
for num in nums:
10+
if len(seq) == 0:
11+
seq.append(num)
12+
continue
13+
14+
i = 0
15+
16+
while seq[i] < num:
17+
i = i + 1
18+
19+
if i > len(seq) - 1:
20+
break
21+
22+
if i > len(seq) - 1:
23+
seq.append(num)
24+
else:
25+
seq[i] = num
26+
27+
return len(seq)

spiral-matrix/sangbeenmoon.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 우 -> 하 -> 좌 -> 상
2+
3+
class Solution:
4+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
5+
dx = [1, 0, -1, 0]
6+
dy = [0, 1, 0, -1]
7+
8+
rows, cols = len(matrix), len(matrix[0])
9+
10+
visited = [[False] * cols for _ in range(rows)]
11+
12+
answer = []
13+
14+
def dfs(xx: int, yy:int, d:int):
15+
16+
i = d
17+
cnt = 0
18+
while cnt <= 5:
19+
nx = xx + dx[i]
20+
ny = yy + dy[i]
21+
22+
if 0 <= nx and nx < cols and 0 <= ny and ny < rows:
23+
if not visited[ny][nx]:
24+
visited[ny][nx] = True
25+
answer.append(matrix[ny][nx])
26+
dfs(nx,ny,i)
27+
return
28+
if i == 3:
29+
i = 0
30+
else:
31+
i = i + 1
32+
cnt = cnt + 1
33+
34+
35+
36+
visited[0][0] = True
37+
answer.append(matrix[0][0])
38+
dfs(0,0,0)
39+
40+
return answer

0 commit comments

Comments
 (0)