Skip to content

Commit c1bf996

Browse files
authored
Merge pull request #1655 from yyyyyyyyyKim/main
[yyyyyyyyyKim] WEEK 15 solutions
2 parents 81854bf + e5260e7 commit c1bf996

File tree

5 files changed

+250
-0
lines changed

5 files changed

+250
-0
lines changed

โ€Žalien-dictionary/yyyyyyyyyKim.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
LintCode 892. Alien Dictionary
3+
https://www.lintcode.com/problem/892/
4+
https://leetcode.com/problems/alien-dictionary/
5+
6+
summary:
7+
์™ธ๊ณ„์ธ ๋‹จ์–ด ๋ชฉ๋ก์ด ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ์„ ๋•Œ,
8+
๋‹จ์–ด ๊ฐ„์˜ ๋น„๊ต๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์˜ ์ˆœ์„œ๋ฅผ ์œ ์ถ”ํ•ด ์ „์ฒด ์™ธ๊ณ„์ธ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
9+
"""
10+
from typing import (
11+
List,
12+
)
13+
14+
class Solution:
15+
"""
16+
@param words: a list of words
17+
@return: a string which is correct order
18+
"""
19+
def alien_order(self, words: List[str]) -> str:
20+
21+
# ๋ฌด์กฐ๊ฑด๋‹ค์‹œํ’€์–ด๋ณผ๊ฒƒ. ์–ด๋ ค์›€์–ด๋ ค์›€.
22+
# DFS๊ธฐ๋ฐ˜ ํ›„์œ„์ˆœํšŒ(post-order) ์œ„์ƒ ์ •๋ ฌ
23+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(N*L+V+E), ๊ณต๊ฐ„๋ณต์žก๋„ O(V+E)
24+
# V = ๋“ฑ์žฅํ•œ ๋ฌธ์ž์˜ ์ˆ˜(์ตœ๋Œ€26๊ฐœ), E = ๊ฐ„์„ ์ˆ˜(๋ฌธ์ž๊ฐ„์ˆœ์„œ), N = ๋‹จ์–ด์ˆ˜, L = ๊ฐ ๋‹จ์–ด์˜ ํ‰๊ท  ๊ธธ์ด
25+
26+
# ๊ทธ๋ž˜ํ”„ ๋งŒ๋“ค๊ธฐ(๋ฌธ์ž๋ณ„ ์—ฐ๊ฒฐ ์ •๋ณด)
27+
graph = {}
28+
29+
# visited
30+
# 0: ๋ฐฉ๋ฌธ ์•ˆํ•จ
31+
# 1: ๋ฐฉ๋ฌธ ์ค‘(ํ˜„์žฌ ํƒ์ƒ‰ ์ค‘)
32+
# 2: ๋ฐฉ๋ฌธ ์™„๋ฃŒ(์‚ฌ์ดํด ์—†์Œ ํ™•์ธ ์™„๋ฃŒ)
33+
visited = {}
34+
answer = []
35+
36+
# ๋…ธ๋“œ ์ดˆ๊ธฐํ™”(๋ชจ๋“  ๋ฌธ์ž ๊ทธ๋ž˜ํ”„์— ๋„ฃ๊ธฐ)
37+
for word in words:
38+
for i in word:
39+
if i not in graph: # ์—†์œผ๋ฉด ๋„ฃ๊ธฐ(์ค‘๋ณต๋ฐฉ์ง€)
40+
graph[i] = []
41+
42+
# ๋ฌธ์ž ๊ฐ„์„  ์„ค์ •
43+
for i in range(len(words)-1):
44+
w1 = words[i]
45+
w2 = words[i+1]
46+
min_len = min(len(w1), len(w2))
47+
48+
# ์•ž๋‹จ์–ด๊ฐ€ ๋’ท๋‹จ์–ด์˜ ์ ‘๋‘์‚ฌ์ธ๋ฐ ๋” ๊ธธ๋ฉด ์ž˜๋ชป๋œ ์ž…๋ ฅ์ด๋ฏ€๋กœ '' ๋ฐ˜ํ™˜
49+
if w1[:min_len] == w2[:min_len] and len(w1) > len(w2):
50+
return ''
51+
52+
for j in range(min_len):
53+
c1 = w1[j]
54+
c2 = w2[j]
55+
# c1๊ณผ c2๊ฐ€ ๋‹ค๋ฅธ ๋ฌธ์ž์ด๊ณ  c1์— c2๊ฐ€ ์—†์œผ๋ฉด c1์˜ ๊ทธ๋ž˜ํ”„์— c2 ์ถ”๊ฐ€
56+
if c1 != c2:
57+
if c2 not in graph[c1]:
58+
graph[c1].append(c2)
59+
break
60+
61+
# ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ, ์‚ฌ์ „์ˆœ ํƒ์ƒ‰์„ ์œ„ํ•ด ์ž์‹ ๋…ธ๋“œ ์ •๋ ฌ
62+
for i in graph:
63+
graph[i].sort()
64+
65+
# DFS ํ•จ์ˆ˜
66+
def dfs(node):
67+
# visited.get(node, 0) : ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’ 0 ๋ฐ˜ํ™˜
68+
if visited.get(node, 0) == 1:
69+
return False # ์‚ฌ์ดํด ๋ฐœ์ƒ
70+
if visited.get(node, 0) == 2:
71+
return True # ๋ฐฉ๋ฌธ ์™„๋ฃŒ
72+
73+
# ๋ฐฉ๋ฌธ ์ค‘ ํ‘œ์‹œ
74+
visited[node] = 1
75+
76+
# ์ธ์ ‘ ๋…ธ๋“œ(๋‹ค์Œ๋ฌธ์ž) ํƒ์ƒ‰
77+
for i in graph[node]:
78+
if not dfs(i):
79+
return False
80+
81+
# ํƒ์ƒ‰ ์™„๋ฃŒ(๋ฐฉ๋ฌธ ์™„๋ฃŒ)
82+
visited[node] = 2
83+
answer.append(node)
84+
85+
return True
86+
87+
# ๋ชจ๋“  ๋…ธ๋“œ dfs ํƒ์ƒ‰
88+
for i in graph:
89+
if visited.get(i, 0) == 0:
90+
if not dfs(i):
91+
return '' # ์‚ฌ์ดํด์ด๋ฉด ์ž˜๋ชป๋œ ์ž…๋ ฅ
92+
93+
94+
# post-order๋‹ˆ๊นŒ ์—ญ์ˆœ์œผ๋กœ ๋ฐ˜ํ™˜
95+
return ''.join(reversed(answer))
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
3+
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
4+
5+
summary:
6+
์ „์œ„์ˆœํšŒ(preorder)์™€ ์ค‘์œ„์ˆœํšŒ(inorder)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด์ง„ํŠธ๋ฆฌ ๊ตฌ์„ฑํ•˜๊ธฐ
7+
"""
8+
# Definition for a binary tree node.
9+
# class TreeNode:
10+
# def __init__(self, val=0, left=None, right=None):
11+
# self.val = val
12+
# self.left = left
13+
# self.right = right
14+
class Solution:
15+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
16+
17+
# DFS
18+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n), ๊ณต๊ฐ„๋ณต์žก๋„ ์ตœ์•…O(n)/ํ‰๊ท O(log n) (n = ๋…ธ๋“œ์ˆ˜)
19+
self.preorder_idx = 0 # preorder์— ํ˜„์žฌ ์œ„์น˜ ์ธ๋ฑ์Šค(์ฒ˜์Œ๊ฐ’ = ๋ฃจํŠธ๋…ธ๋“œ๊ฐ’)
20+
21+
inorder_map = {} # inorder ๊ฐ’์„ ์ธ๋ฑ์Šค๋กœ ๋งคํ•‘(๋น ๋ฅธ ๊ฒ€์ƒ‰์„ ์œ„ํ•ด)
22+
for i in range(len(inorder)):
23+
inorder_map[inorder[i]] = i
24+
25+
26+
def dfs(left: int, right:int) -> Optional[TreeNode]:
27+
# ์™ผ์ชฝ์ธ๋ฑ์Šค๊ฐ€ ์˜ค๋ฅธ์ชฝ์ธ๋ฑ์Šค๋ณด๋‹ค ํฌ๋ฉด(๋ฒ”์œ„๋ฅผ๋ฒ—์–ด๋‚˜๋ฉด) ๋นˆ ์„œ๋ธŒํŠธ๋ฆฌ -> ์ข…๋ฃŒ
28+
if left > right:
29+
return None
30+
31+
# ํ˜„์žฌ preorder์—์„œ ํ˜„์žฌ ๋ฃจํŠธ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ณ  ํ•œ ์นธ ์ด๋™
32+
root_val = preorder[self.preorder_idx]
33+
self.preorder_idx += 1
34+
35+
# ๋ฃจํŠธ ๋…ธ๋“œ ์ƒ์„ฑ
36+
root = TreeNode(root_val)
37+
38+
# inorder์—์„œ ํ˜„์žฌ ๋ฃจํŠธ ์œ„์น˜ ์ฐพ๊ธฐ
39+
i = inorder_map[root_val]
40+
41+
# ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ๊ตฌ์„ฑ
42+
root.left = dfs(left, i-1)
43+
root.right = dfs(i+1, right)
44+
45+
return root
46+
47+
# ์ „์ฒด inorder dfs ๋Œ๊ธฐ
48+
return dfs(0,len(inorder)-1)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
LeetCode 5. Longest Palindromic Substring
3+
https://leetcode.com/problems/longest-palindromic-substring/
4+
5+
summary:
6+
์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s์—์„œ "๊ฐ€์žฅ ๊ธด ํŒฐ๋ฆฐ๋“œ๋ฃธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด"์„ ์ฐพ์•„ ๋ฐ˜ํ™˜
7+
"""
8+
9+
class Solution:
10+
def longestPalindrome(self, s: str) -> str:
11+
12+
# DP (์‹œ๊ฐ„๋ณต์žก๋„ O(n^2), ๊ณต๊ฐ„๋ณต์žก๋„ O(n^2))
13+
n = len(s)
14+
dp = [[False]*n for _ in range(n)] # dp[i][j] = s[i..j]์˜ ํŒฐ๋ฆฐ๋“œ๋ฃธ ์—ฌ๋ถ€ ์ €์žฅ
15+
answer = ''
16+
17+
# i = ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด(1๋ถ€ํ„ฐ n๊นŒ์ง€)
18+
for i in range(1,n+1):
19+
# j = ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค
20+
for j in range(n - i + 1):
21+
# ๋ ์ธ๋ฑ์Šค = ์‹œ์ž‘์ธ๋ฑ์Šค + ๊ธธ์ด - 1
22+
end = j + i -1
23+
# ์–‘ ๋ ๋ฌธ์ž๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ
24+
if s[j] == s[end]:
25+
# ๊ธธ์ด๊ฐ€ 3์ดํ•˜๋ฉด ํŒฐ๋ฆฐ๋“œ๋ฃธ
26+
if i <= 3:
27+
dp[j][end] = True
28+
# ์–‘ ๋์ด ๊ฐ™๊ณ  ์•ˆ์ชฝ๋„ ํŒฐ๋ฆฐ๋“œ๋ฃธ์ด๋ฉด ์ „์ฒด๋„ ํŒฐ๋ฆฐ๋“œ๋ฃธ
29+
else:
30+
dp[j][end] = dp[j+1][end-1]
31+
32+
# ํ˜„์žฌ ํŒฐ๋ฆฐ๋“œ๋ฃธ์˜ ๊ธธ์ด๊ฐ€ answer๋ณด๋‹ค ๊ธธ๋ฉด ์—…๋ฐ์ดํŠธ
33+
if dp[j][end] and i > len(answer):
34+
answer = s[j:end+1]
35+
36+
return answer

โ€Žrotate-image/yyyyyyyyyKim.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
LeetCode 48. Rotate Image
3+
https://leetcode.com/problems/rotate-image/
4+
5+
summary:
6+
n x n ํ–‰๋ ฌ์„ ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ 90๋„ ํšŒ์ „(in-place, ์ถ”๊ฐ€ ๊ณต๊ฐ„์—†์ด ํ–‰๋ ฌ ์ž์ฒด๋ฅผ ์ˆ˜์ •)
7+
"""
8+
class Solution:
9+
def rotate(self, matrix: List[List[int]]) -> None:
10+
"""
11+
Do not return anything, modify matrix in-place instead.
12+
"""
13+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n^2), ๊ณต๊ฐ„๋ณต์žก๋„ O(1)
14+
15+
n = len(matrix)
16+
17+
# ํ–‰๊ณผ ์—ด ๋ฐ”๊พธ๊ธฐ
18+
for i in range(n):
19+
for j in range(i+1, n):
20+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
21+
22+
# ํ–‰์„ ์ขŒ์šฐ๋ฐ˜์ „ํ•˜๊ธฐ
23+
for i in range(n):
24+
for j in range(n//2):
25+
matrix[i][j], matrix[i][n-j-1] = matrix[i][n-j-1], matrix[i][j]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
LeetCode 572. Subtree of Another Tree
3+
https://leetcode.com/problems/subtree-of-another-tree/
4+
5+
summary:
6+
root ํŠธ๋ฆฌ ์•ˆ์— subRoot ํŠธ๋ฆฌ์™€ ๋™์ผํ•œ ๊ตฌ์กฐ & ๊ฐ’์„ ๊ฐ€์ง„ ์„œ๋ธŒํŠธ๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
7+
"""
8+
9+
# Definition for a binary tree node.
10+
# class TreeNode:
11+
# def __init__(self, val=0, left=None, right=None):
12+
# self.val = val
13+
# self.left = left
14+
# self.right = right
15+
class Solution:
16+
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
17+
18+
# DFS
19+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n*m) : n = rootํŠธ๋ฆฌ ๋…ธ๋“œ ๊ฐœ์ˆ˜, m = subRootํŠธ๋ฆฌ ๋…ธ๋“œ ๊ฐœ์ˆ˜
20+
# ๊ณต๊ฐ„๋ณต์žก๋„ O(h1+h2) : h1 = root์˜ ์ตœ๋Œ€๋†’์ด(์ตœ์•…O(n)), h2 = subRoot์˜ ์ตœ๋Œ€๋†’์ด(์ตœ์•…O(m))
21+
# ๋‘ ํŠธ๋ฆฌ์˜ ๊ตฌ์กฐ์™€ ๊ฐ’์ด ๊ฐ™์€์ง€ ํ™•์ธ
22+
def isSameTree(node1, node2):
23+
# ๋‘˜ ๋‹ค ์—†์œผ๋ฉด True
24+
if not node1 and not node2:
25+
return True
26+
# ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ์—†์œผ๋ฉด False
27+
if not node1 or not node2:
28+
return False
29+
# ํ˜„์žฌ ๋…ธ๋“œ ๊ฐ’์ด ๋‹ค๋ฅด๋ฉด False
30+
if node1.val != node2.val:
31+
return False
32+
33+
# root ํŠธ๋ฆฌ๋ž‘ subRoot ํŠธ๋ฆฌ์˜ ์–‘์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ์žฌ๊ท€ ํƒ์ƒ‰
34+
return isSameTree(node1.left, node2.left) and isSameTree(node1.right, node2.right)
35+
36+
# root ํŠธ๋ฆฌ๋ฅผ DFS๋กœ ๋Œ๋ฉด์„œ subRoot์™€ ๊ฐ™์€์ง€ ํ™•์ธ
37+
def dfs(node):
38+
if not node:
39+
return False
40+
if isSameTree(node, subRoot):
41+
return True
42+
43+
# root ํŠธ๋ฆฌ์˜ ์–‘์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ํƒ์ƒ‰
44+
return dfs(node.left) or dfs(node.right)
45+
46+
return dfs(root)

0 commit comments

Comments
ย (0)