Skip to content

Commit 8103f51

Browse files
authored
Merge pull request #1966 from hu6r1s/main
[hu6r1s] WEEK 15 Solutions
2 parents ef4281b + 01bbd93 commit 8103f51

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

alien-dictionary/hu6r1s.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import (
2+
List,
3+
)
4+
5+
import heapq
6+
heap = [c for c in indegree if indegree[c] == 0]
7+
heapq.heapify(heap)
8+
res = []
9+
10+
11+
class Solution:
12+
"""
13+
@param words: a list of words
14+
@return: a string which is correct order
15+
"""
16+
def alien_order(self, words: List[str]) -> str:
17+
# Write your code here
18+
adj = {c: set() for word in words for c in word}
19+
indegree = {c: 0 for c in adj}
20+
21+
for i in range(len(words) - 1):
22+
w1, w2 = words[i], words[i+1]
23+
minlen = min(len(w1), len(w2))
24+
if len(w1) > len(w2) and w1[:minlen] == w2[:minlen]:
25+
return ""
26+
for j in range(minlen):
27+
if w1[j] != w2[j]:
28+
if w2[j] not in adj[w1[j]]:
29+
adj[w1[j]].add(w2[j])
30+
indegree[w2[j]] += 1
31+
break
32+
33+
while heap:
34+
c = heapq.heappop(heap)
35+
res.append(c)
36+
for nei in adj[c]:
37+
indegree[nei] -= 1
38+
if indegree[nei] == 0:
39+
heapq.heappush(heap, nei)
40+
41+
if len(res) != len(adj):
42+
return ""
43+
44+
return "".join(res)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
9+
if not inorder:
10+
return None
11+
12+
val = preorder.pop(0)
13+
mid = inorder.index(val)
14+
left = self.buildTree(preorder, inorder[:mid])
15+
right = self.buildTree(preorder, inorder[mid + 1 :])
16+
return TreeNode(val, left, right)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def longestPalindrome(self, s: str) -> str:
3+
# if len(s) <= 2:
4+
# return s[0]
5+
6+
# for i in range(2, len(s)):
7+
# for k in range(len(s)-i):
8+
# if s[k:k+i] == s[k:k+i][::-1]:
9+
# return s[k:k+i]
10+
11+
max_s, max_e = 0, 0
12+
13+
for i in range(len(s)):
14+
start, end = i, i
15+
while 0 <= start and end < len(s) and s[start] == s[end]:
16+
if max_e - max_s < end - start:
17+
max_s, max_e = start, end
18+
start, end = start - 1, end + 1
19+
20+
start, end = i, i + 1
21+
while 0 <= start and end < len(s) and s[start] == s[end]:
22+
if max_e - max_s < end - start:
23+
max_s, max_e = start, end
24+
start, end = start - 1, end + 1
25+
26+
return s[max_s : max_e + 1]

rotate-image/hu6r1s.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def rotate(self, matrix: List[List[int]]) -> None:
3+
"""
4+
Do not return anything, modify matrix in-place instead.
5+
[0][0] [0][1] [0][2]
6+
[1][0] [1][1] [1][2]
7+
[2][0] [2][1] [2][2]
8+
9+
[2][0] [1][0] [0][0]
10+
[2][1] [1][1] [0][1]
11+
[2][2] [1][2] [0][2]
12+
"""
13+
n = len(matrix)
14+
15+
for i in range(n):
16+
for j in range(i, n):
17+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
18+
19+
for i in range(n):
20+
matrix[i].reverse()

subtree-of-another-tree/hu6r1s.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
9+
if not subRoot:
10+
return True
11+
if not root:
12+
return False
13+
14+
def same(root, subRoot):
15+
if not root or not subRoot:
16+
return not root and not subRoot
17+
18+
if root.val != subRoot.val:
19+
return False
20+
return same(root.left, subRoot.left) and same(root.right, subRoot.right)
21+
22+
if same(root, subRoot):
23+
return True
24+
return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)

0 commit comments

Comments
 (0)