Zero to Hero
article thumbnail
208. Implement Trie (Prefix Tree)
Algorithm 2021. 5. 18. 10:23

Implement Trie (Prefix 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. Trie 자료구조 구현 class Trie: def __init__(self): self.head = Node() def insert(self, word: str) -> None: cur = self.head for char in word: if char not in cur.child: new_node = Node(char) cur.child[char] =..

article thumbnail
240. Search a 2D Matrix II
Algorithm 2021. 5. 18. 10:03

Search a 2D Matrix II - 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. Brute Force class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: temp = [] for item in matrix: temp += item if target in temp: return True return False 그냥 이렇게 배열을 정말 탐색해도 통과는..

article thumbnail
279. Perfect Squares
Algorithm 2021. 5. 17. 13:14

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[..

article thumbnail
236. Lowest Common Ancestor of a Binary Tree
Algorithm 2021. 5. 17. 10:57

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..

article thumbnail
200. Number of Islands
Algorithm 2021. 5. 17. 09:58

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..

article thumbnail
75. Sort Colors
Algorithm 2021. 5. 16. 11:48

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..