Zero to Hero
article thumbnail
621. Task Scheduler
Algorithm 2021. 5. 14. 11:34

Task Scheduler - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 내 Trash Garbage Code from typing import * from heapq import * from collections import defaultdict class Solution: def leastInterval(self, tasks: List[str], n: int) -> int: dict1 = defaultdict(int) for task in tasks..

article thumbnail
114. Flatten Binary Tree to Linked List
Algorithm 2021. 5. 13. 15:35

Flatten Binary Tree to Linked List - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Post - Order class Solution: def flatten(self, root: TreeNode) -> None: stack, temp = [], [] cur = root while True: while cur: temp.append(cur) stack.append(cur) cur = cur.left if not stack: bre..

article thumbnail
62. Unique Paths
Algorithm 2021. 5. 12. 15:13

1. Dynamic Programming class Solution: def uniquePaths(self, height: int, width: int) -> int: board = [[0] * width for _ in range(height)] for i in range(height): board[i][0] = 1 for i in range(width): board[0][i] = 1 for i in range(1, height): for j in range(1, width): board[i][j] = board[i - 1][j] + board[i][j - 1] return board[-1][-1] 예전엔 DP라고 생각도 안 났던 것 같은데 쉬운 문제지만 그래도 장족의 발전이라고 생각한다 ㅎㅎ;

article thumbnail
64. Minimum Path Sum
Algorithm 2021. 5. 12. 12:48

Minimum Path Sum - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Stack을 이용한 DFS(TLE) from typing import * class Solution: def minPathSum(self, grid: List[List[int]]) -> int: answer = float("inf") s_x, s_y = 0, 0 t_x, t_y = len(grid) - 1, len(grid[0]) - 1 height, width = t_x + ..

article thumbnail
199. Binary Tree Right Side View
Algorithm 2021. 5. 11. 11:10

Binary Tree Right Side View - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Queue를 이용한 풀이 class Solution: def rightSideView(self, root: TreeNode) -> List[int]: if not root: return [] answer, trace = [], defaultdict(int) q = deque([(root, 0)]) while q: cur_node, cur_level = q.p..

article thumbnail
102. Binary Tree Level Order Traversal
Algorithm 2021. 5. 11. 10:32

Binary Tree Level Order Traversal - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Queue를 사용한 풀이 class Solution: def levelOrder(self, root: TreeNode) -> List[List[int]]: answer = [] if not root: return [] q = deque([]) q.append((root, 0)) trace = {} while q: cur_node, cur_level..