Zero to Hero
article thumbnail
11. Container With Most Water
Algorithm 2021. 6. 30. 22:16

Container With Most Water - 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 컨테이너 벽의 값이 list로 주어진다. 두 개의 값을 골라서 가장 많은 양의 물을 담을 수 있는 컨테이너의 세로 벽 후보를 고르고, 그때 담을 수 있는 물의 양을 반환하는 문제다. 1. Brute Force from typing import * class Solution: def maxArea(self, height: List[int]) -> int: set1 = ..

article thumbnail
96. Unique Binary Search Trees
Algorithm 2021. 6. 27. 17:18

Unique Binary Search Trees - 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 처음에 DFS로 해결할 수 있는지를 생각해보았다. 결과적으론 불가능하다고 판단했다. 루트 노드를 기준으로 DFS는 left subtree, right subtree를 한 재귀에서 위 문제에서 확인해야 하는 상태를 볼 수 없다. 2. DP cl..

article thumbnail
380. Insert Delete GetRandom O(1)
Algorithm 2021. 6. 26. 16:55

Insert Delete GetRandom O(1) - 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 class RandomizedSet: def __init__(self): """ Initialize your data structure here. """ def insert(self, val: int) -> bool: """ Inserts a value to the set. Returns true if the set did not already contain t..

article thumbnail
116. Populating Next Right Pointers in Each Node
Algorithm 2021. 6. 24. 17:22

Populating Next Right Pointers in Each Node - 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, defaultdict class Solution: def connect(self, root: 'Node') -> 'Node': q = deque([(root, 0)]) dict1 = d..

article thumbnail
103. Binary Tree Zigzag Level Order Traversal
Algorithm 2021. 6. 20. 22:37

Binary Tree Zigzag 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 이진트리가 주어진다. root level을 0이라고 했을 때 0, 2, 4, 6,.... 레벨의 노드는 왼쪽에서 오른쪽으로 순회하고, 1, 3, 5, 7,... 레벨의 노드는 오른쪽에서 왼쪽으로 순회한 결과를 반환하는 문제다. 1. Deque, BFS를 사용한 풀이 from typing import * from collections impo..

article thumbnail
36. Valid Sudoku
Algorithm 2021. 6. 20. 00:13

Valid Sudoku - 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. 뇌를 비운 풀이 class Solution: def isValidSudoku(self, board): for i in range(len(board)): board[i] = list(map(lambda x: int(x) if x.isdigit() else 0, board[i])) rows, ..