π Leetcode 78 Subsets - Medium
- λ΄ νμ΄
class Solution: def subsets(self, nums: List[int]) -> List[List[int]]: result = [] def dfs(num_list, index): result.append(num_list[:]) for i in range(index, len(nums)): num_list.append(nums[i]) dfs(num_list, i+1) num_list.pop() dfs([], 0) return result
- μ± νμ΄
class Solution: def subsets(self, nums: List[int]) -> List[List[int]]: result = [] def dfs(index, path): result.append(path) for i in range(index, len(nums)): dfs(i+1, path + [nums[i]]) dfs(0, []) return result
κΈ°λ³Έλ§ μλ©΄ ν μ μλ λ¬Έμ λ€.
π Leetcode 332 Reconstruct Itinerary - Hard
- μ²μ νμ΄
class Solution: def findItinerary(self, tickets: List[List[str]]) -> List[str]: result = [] visited = [0 for _ in range(len(tickets))] tickets.sort() def dfs(path, visit): if len(path) == len(tickets) + 1: result.append(path[:]) for index, val in enumerate(tickets): if val[0] == path[-1] and visit[index] == 0: path.append(val[1]) visit[index] = 1 dfs(path, visit[:]) path.pop() visit[index] = 0 dfs(["JFK"], visited) result.sort() return result[0]
μ²μ νμλ νμ΄μΈλ° μκ°μ΄κ³Ό λ νμ΄μ΄λ€.
visitedλ₯Ό λ§λ€μ΄ λͺ©μ μ§κΉμ§μ λͺ¨λ κ²½μ°λ₯Ό λ§λ€κ³ μ¬μ μμΌλ‘ μ λ ¬ν ν
κ°μ₯ μμμλ κ°μ λ°ννλ€.
νμ§λ§
- μ± stack νμ΄
class Solution: def findItinerary(self, tickets: List[List[str]]) -> List[str]: from collections import defaultdict graph = defaultdict(list) tickets.sort(reverse=True) for a, b in tickets: graph[a].append(b) result = [] stack = ["JFK"] while stack: while graph[stack[-1]]: stack.append(graph[stack[-1]].pop()) result.append(stack.pop()) return result[::-1]
μ΄ λ¬Έμ λ₯Ό νλ©΄μ κ°μ₯ κ³ λ €ν΄μΌ νλ λΆλΆμ΄ μ¬μ μμΌλ‘ μ λ ¬ ν λμ΄νμλ λ€μ
νμ μ§κ° λκΈ°λ κ²½μ°μλλ°result.append(stack.pop())
λ§νλ λΆλΆμμ λΉΌμ€λ€.
- DFS νμ΄
class Solution: def findItinerary(self, tickets: List[List[str]]) -> List[str]: from collections import defaultdict graph = defaultdict(list) tickets.sort(key=lambda x:x[1], reverse=True) for start, end in tickets: graph[start].append(end) result = [] def dfs(start): while graph[start]: dfs(graph[start].pop()) result.append(start) dfs("JFK") return result[::-1]
μ΄ν΄νλλ° μ’ κ±Έλ Έμ§λ§ λ§νλ λΆλΆμ μμ λ₯Ό κ·Έλ €κ°λ©° 보면 μ΄ν΄κ° λλ€.
νμ§λ§ μ΄ νμ΄λλ‘ λ€μ ν μ μμκΉ μΆλ€...
λ§μ΄ λ νμ΄λ΄μΌκ² λ€.
π Leetcode 112 Path Sum - Easy
# 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 hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: if not root: return False if not root.left and not root.right and targetSum - root.val == 0: return True return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(root.right, targetSum - root.val)
μ΄κ±΄ μ½λ€ μ¬κ·λ‘ νμλ€.
π Leetcode 98 Validate Binary Search Tree - Medium
# 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 isValidBST(self, root: Optional[TreeNode]) -> bool: import math return self.Recursion(root, -math.inf, math.inf) def Recursion(self, node, min, max): if not node: return True if node.val <= min or node.val >= max: return False return self.Recursion(node.left, min, node.val) and self.Recursion(node.right, node.val, max)
μ²μμ ν¨μλ₯Ό νλ λ§λ€μ§ μκ³ νλ €λ€κ° λͺ»νκ³ ννΈλ₯Ό λ³΄κ³ ν¨μλ₯Ό νλ λ λ§λ€μ΄μ νμλ€. μ λ¬Έμ λ₯Ό μ¬κ·λ‘ νμμ΄μ μ΄ λ¬Έμ λ λκ°μ΄ μλν΄λ΄€λλ° min, maxλ₯Ό κ³μ λκΈΈ μ μμ΄μ μμκ°μ΄ νμλ€.