Zero to Hero
article thumbnail
338. Counting Bits
Algorithm 2021. 4. 9. 11:24

Counting Bits - 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. 내장 함수를 이용한 탐색 def countBits(self, num: int) -> List[int]: answer = [] for i in range(num + 1): answer.append(format(i, "b").count("1")) return answer 2. 비트의 성질을 이용한 반복 사용 def countBits(self, num: int) -> List[int]: ..

article thumbnail
310. Minimum Height Trees
Algorithm 2021. 4. 8. 14:57

Minimum Height 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. Brute Force DFS (시간 초과) from typing import * from collections import defaultdict class Solution: def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]: graph = defaultdict(set) for edge in..

article thumbnail
287. Find the Duplicate Number
Algorithm 2021. 4. 7. 11:30

Find the Duplicate 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. 선형 탐색 (시간 초과) class Solution: def findDuplicate(self, nums): nums.sort() for i in range(1, len(nums)): if nums[i] == nums[i-1]: return nums[i] 2. HashMap or Set (공간 초과) class Solution: def findDuplicate(..

article thumbnail
238. Product of Array Except Self
Algorithm 2021. 4. 6. 13:48

Product of Array Except Self - 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 productExceptSelf(self, nums: List[int]) -> List[int]: answer = [] temp1 = [1] for i in range(1, len(nums)): temp1.append(t..

article thumbnail
46. Permutations
Algorithm 2021. 4. 5. 16:00

Permutations - 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 from typing import * from copy import deepcopy class Solution: def permute(self, nums: List[int]) -> List[List[int]]: answer = [] visited = [0] * len(nums) def sol(permu_list, visited_list): if len(permu_lis..

article thumbnail
94. Binary Tree Inorder Traversal
Algorithm 2021. 4. 5. 14:09

Binary Tree 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 1. DFS를 이용한 Inorder 구현 from typing import * class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: answer = [] def move(cur): if cur.left: move(cur.left) answer.append(cur.val) if cur...