Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions alien-dictionary/hu6r1s.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와! 다른 문제는 어떤 건지 이해 했는데 이건 해답을 봐도 모르겠네요... heap 자료구조까지 써야 하는 문제가 있는 줄은...
수고 많으셨습니다~

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import (
List,
)

import heapq
heap = [c for c in indegree if indegree[c] == 0]
heapq.heapify(heap)
res = []


class Solution:
"""
@param words: a list of words
@return: a string which is correct order
"""
def alien_order(self, words: List[str]) -> str:
# Write your code here
adj = {c: set() for word in words for c in word}
indegree = {c: 0 for c in adj}

for i in range(len(words) - 1):
w1, w2 = words[i], words[i+1]
minlen = min(len(w1), len(w2))
if len(w1) > len(w2) and w1[:minlen] == w2[:minlen]:
return ""
for j in range(minlen):
if w1[j] != w2[j]:
if w2[j] not in adj[w1[j]]:
adj[w1[j]].add(w2[j])
indegree[w2[j]] += 1
break

while heap:
c = heapq.heappop(heap)
res.append(c)
for nei in adj[c]:
indegree[nei] -= 1
if indegree[nei] == 0:
heapq.heappush(heap, nei)

if len(res) != len(adj):
return ""

return "".join(res)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if not inorder:
return None

val = preorder.pop(0)
mid = inorder.index(val)
left = self.buildTree(preorder, inorder[:mid])
right = self.buildTree(preorder, inorder[mid + 1 :])
return TreeNode(val, left, right)
26 changes: 26 additions & 0 deletions longest-palindromic-substring/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:
def longestPalindrome(self, s: str) -> str:
# if len(s) <= 2:
# return s[0]

# for i in range(2, len(s)):
# for k in range(len(s)-i):
# if s[k:k+i] == s[k:k+i][::-1]:
# return s[k:k+i]

max_s, max_e = 0, 0

for i in range(len(s)):
start, end = i, i
while 0 <= start and end < len(s) and s[start] == s[end]:
if max_e - max_s < end - start:
max_s, max_e = start, end
start, end = start - 1, end + 1

start, end = i, i + 1
while 0 <= start and end < len(s) and s[start] == s[end]:
if max_e - max_s < end - start:
max_s, max_e = start, end
start, end = start - 1, end + 1

return s[max_s : max_e + 1]
20 changes: 20 additions & 0 deletions rotate-image/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]

[2][0] [1][0] [0][0]
[2][1] [1][1] [0][1]
[2][2] [1][2] [0][2]
"""
n = len(matrix)

for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

for i in range(n):
matrix[i].reverse()
24 changes: 24 additions & 0 deletions subtree-of-another-tree/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if not subRoot:
return True
if not root:
return False

def same(root, subRoot):
if not root or not subRoot:
return not root and not subRoot

if root.val != subRoot.val:
return False
return same(root.left, subRoot.left) and same(root.right, subRoot.right)

if same(root, subRoot):
return True
return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)