Zero to Hero
article thumbnail
105. Construct Binary Tree from Preorder and Inorder Traversal
Algorithm 2021. 6. 15. 23:04

Construct Binary Tree from Preorder and Inorder 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 이진트리의 PreOrder 순회 결과와 InOrder 순회 결과를 입력하면 이진트리를 복원해서 반환하는 문제다. 1. 4개의 index 값을 이용한 DFS from typing import * class Solution: def buildTree(self, preorder: List[int], inorder: ..

lower_bound, upper_bound
Algorithm 2021. 6. 15. 10:20

def lower_bound(input_list, target_value): if not input_list: return 0 lo, hi = 0, len(input_list) while lo < hi: mid = (lo + hi) // 2 if input_list[mid] < target_value: lo = mid + 1 else: hi = mid return lo def upper_bound(input_list, target_value): if not input_list: return 0 lo, hi = 0, len(input_list) while lo < hi: mid = (lo + hi) // 2 if target_value < input_list[mid]: hi = mid else: lo = ..

article thumbnail
384. Shuffle an Array
Algorithm 2021. 6. 14. 21:42

Shuffle an Array - 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 Solution: def __init__(self, nums: List[int]): def reset(self) -> List[int]: """ Resets the array to its original configuration and return it. """ def shuffle(self) -> List[int]: """ Returns a random shuffling..

article thumbnail
454. 4Sum II
Algorithm 2021. 6. 13. 14:10

4Sum 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 * class Solution: def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int: set1, set2 = set(), set() for num1 in nums1: for num2 in nums2: set1.add(num1 ..

article thumbnail
341. Flatten Nested List Iterator
Algorithm 2021. 6. 13. 00:21

Flatten Nested List Iterator - 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 # """ # This is the interface that allows for creating nested lists. # You should not implement it, or speculate about its implementation # """ #class NestedInteger: # def isInteger(self) -> bool: # """ ..

article thumbnail
172. Factorial Trailing Zeroes
Algorithm 2021. 6. 10. 17:53

Factorial Trailing Zeroes - 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 trailingZeroes(self, n: int) -> int: return n // 5 N이 주어지고 N! 의 0의 개수를 반환하는 문제다. 처음에는 DP 인가라고 생각을 했다가 좀 더 쉽게 생각할 수 있는 문제인 것 같았다. 0이 만들어지려면 10이 곱해져야 한다. 그리고 10은 2와 5의 곱이다. 즉 2와 5..