Zero to Hero
article thumbnail
378. Kth Smallest Element in a Sorted Matrix
Algorithm 2021. 5. 25. 14:18

Kth Smallest Element in a Sorted Matrix - 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 kthSmallest(self, matrix: List[List[int]], k: int) -> int: temp = [] for item in matrix: temp += item return sorted(temp)[k-1] 2차원 배열에서 k번째로 큰 수를 반환하는 문제다. 그냥 2차..

article thumbnail
289. Game of Life
Algorithm 2021. 5. 24. 13:39

Game of Life - 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 * ''' Any live cell with fewer than two live neighbors dies as if caused by under-population. 8방으로 이웃이 2개 미만이면 죽인다 Any live cell with two or three live neighbors lives on to the next generation...

article thumbnail
191. Number of 1 Bits
Algorithm 2021. 5. 23. 22:29

Number of 1 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. Bit 성질을 이용한 풀이 class Solution: def hammingWeight(self, n: int) -> int: answer = 0 while n: n = n & (n - 1) answer += 1 return answer 비트 조작은 굉장히 재밌는 성질들이 많다. 내가 아는 몇 안 되는 비트 조작 중 하나가 이것인데, 양의 정수 a와 a-1을 AND 연산하면 양..

article thumbnail
108. Convert Sorted Array to Binary Search Tree
Algorithm 2021. 5. 23. 21:49

Convert Sorted Array to Binary Search 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 sortedArrayToBST(self, nums: List[int]) -> TreeNode: def dfs(cur_list): if not cur_list: return None mid = len(cur_list) // 2 cur_node = TreeNode(cur_list[mid]) c..

article thumbnail
300. Longest Increasing Subsequence
Algorithm 2021. 5. 19. 20:43

Longest Increasing Subsequence - 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 bisect import bisect_left class Solution: def lengthOfLIS(self, nums: List[int]) -> int: stack = [] for num in nums: if not stack: stack.append(num) else:..

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