Zero to Hero
article thumbnail
938. Range Sum of BST
Algorithm 2021. 8. 3. 09:36

Range Sum of BST - 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 이진 탐색 트리의 low, high 값을 주면 low 이상 high 이하의 모든 노드의 값을 더해서 반환하는 문제다. 1. DFS, 완전 탐색 class Solution: answer = 0 def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int: def dfs(cur): if low

article thumbnail
814. Binary Tree Pruning
Algorithm 2021. 8. 1. 00:00

Binary Tree Pruning - 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 노드의 값이 0, 1로만 이루어진 이진트리가 주어진다. 주어진 이진트리 중 0으로만 이루어진 가지를 가지치기(pruning)하고 루트 노드를 반환하는 문제다. 1. Recursion class Solution: def pruneTree(self, root: TreeNode) -> TreeNode: def dfs(cur): if not cur.left and not cur.rig..

article thumbnail
1038. Binary Search Tree to Greater Sum Tree
Algorithm 2021. 7. 31. 16:31

Binary Search Tree to Greater Sum 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 이진 탐색 트리가 주어진다. 주어진 이진 탐색 트리 노드들의 값을 (기존 값 + 자신보다 큰 값을 가진 노드들의 값의 합)으로 변환한 뒤 root를 반환하는 문제다. 새로운 트리를 만드는 것이 아니라 기존 트리를 조작해서 문제 조건대로 만든 뒤 입력받은 root를 그대로 반환해야 한다. 1. Recursion 01 class Solution(o..

807. Max Increase to Keep City Skyline
Algorithm 2021. 7. 30. 22:01

Max Increase to Keep City Skyline - 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차원 배열이 주어진다. 2차원 배열을 4방으로 보았을 때 생기는 등고선(skyline)을 유지하면서 최대로 올릴 수 있는 건물의 사이즈를 반환하는 문제다. 예를 들어서 [5,2]라는 1차원 배열을 왼쪽에서 바라보면 5만 보일 것이고, 2가 있는 자리에 3을 더해서 [5, 5]로 만들어도 보이는 모습은 같을 것이다. 이걸 2차원 배열의 경우로 생각할 때..

article thumbnail
210. Course Schedule II
Algorithm 2021. 7. 28. 09:23

Course Schedule 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. 위상 정렬 from typing import * from collections import defaultdict, deque class Solution: def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> Lis..

article thumbnail
307. Range Sum Query - Mutable
Algorithm 2021. 7. 25. 10:59

Range Sum Query - Mutable - 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가 주어질 때 주석으로 된 코드가 작동하도록 메서드를 구현하는 문제다. class NumArray: def __init__(self, nums: List[int]): # 자유롭게 사용 def update(self, index: int, val: int) -> None: # nums[index]의 값을 val로 바꾸시오 def sumRange(s..