Zero to Hero
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..

article thumbnail
39. Combination Sum
Algorithm 2021. 5. 11. 10:01

Combination 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. 내 Trash Garbage Code from typing import * # DP로 풀어야할 것 같다 # 완전탐색으로 하면 반드시 TLE가 날 것 같은 문제 # 라고 생각했는데 노트에 써보니깐 백트래킹 문제 같다. class Solution: def combinationSum(self, candidates: List[int], target: int) -> List[List[in..

article thumbnail
49. Group Anagrams
Algorithm 2021. 5. 10. 09:25

Group Anagrams - 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. 소팅된 문자열을 Key로 하는 Dict(HashMap)을 이용한 풀이 class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: dict1={} for str in strs: temp="".join(sorted(list(str))) if temp not in dict1: dict1[temp]=[str] ..

article thumbnail
48. Rotate Image
Algorithm 2021. 5. 9. 12:20

Rotate Image - 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 요약 2차원 배열이 주어지면 추가 공간 할당 없이 시계 방향으로 회전시키는 문제 1. Swap class Solution: def rotate(self, matrix: List[List[int]]) -> None: length = len(matrix) for i in range(length // 2 + length % 2): for j in range(length // 2): temp = ..