Perfect Squares - 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. DP 풀이 class Solution: def numSquares(self, n: int) -> int: dp = [0] * (n + 1) for i in range(1, int(math.sqrt(n)) + 1): dp[i * i] = 1 for i in range(1, n + 1): if dp[i]: continue else: temp = int(math.sqrt(i)) dp[..
Lowest Common Ancestor of a Binary Tree - 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. DFS class Solution: def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> 'TreeNode': p_seq, q_seq = [], [] def dfs(cur, seq): nonlocal p_seq, q_seq if not cur: return..
Number of Islands - 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. BFS from collections import deque from typing import * class Solution: def numIslands(self, grid: List[List[str]]) -> int: answer = 0 height, width = len(grid), len(grid[0]) visited = [[0] * width for _ in range..
Sort Colors - 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. sort() class Solution: def sortColors(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ nums.sort() 0,1,2로 이루어진 list를 오름차순 정렬하는 문제다. 이렇게도 그냥 풀린다. 2. Dutch national flag prob..
House Robber III - 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. BFS를 이용한 Level 별 탐색(실패) from collections import defaultdict, deque class Solution: def rob(self, root: TreeNode) -> int: dict1 = defaultdict(int) if not root: return 0 q = deque([(root, 0)]) while q: cur_node, cu..
Letter Combinations of a Phone Number - 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. DFS class Solution(object): def letterCombinations(self, digits): dict1 = {2: ["a", "b", "c"], 3: ["d", "e", "f"], 4: ["g", "h", "i"], 5: ["j", "k", "l"], 6: ["m", "n", "o"], 7: ["p", "q", "r..