Solution) class Solution: def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: res = [] for i in range(len(intervals)): # when newInterval is less than cur interval # and don't overlap if newInterval[1] ..
Solution) class Solution: def merge(self, intervals: List[List[int]]) -> List[List[int]]: # Initialization intervals.sort() res = [intervals[0]] for start, end in intervals[1:]: lastEnd = res[-1][1] # If the current interval overlaps the previous interval if start List[List[int]]: intervals.sort() res = [intervals[0]] for start, end in intervals[1:]: lastEnd = res[-1][1] if start
Solution) class Solution: def longestConsecutive(self, nums: List[int]) -> int: # Initialization mySet = set(nums) res = 0 for n in nums: # length of a sequence length = 1 # If n is the start of a sequence if n-1 not in mySet: cur = n # Find the length of a consecutive sequence while cur+length in mySet: length += 1 # Find the max length of a consecutive sequence res = max(res, length) return re..
Solution) class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: # Initialization dic = collections.defaultdict(list) for word in strs: # Sort words in strs sortedWord = ''.join(sorted(word)) # Group anagrams in dict dic[sortedWord].append(word) return dic.values() Problem: LeetCode Time Complexity: O(m*n*logn) Space Complexity: O(m) * m = len(strs), n = len(words in strs) ..
안녕하심까... 오늘은 힘이 없는 저입니다... 호기롭게 3달안에 리액트 네이티브를 마스터하겠다는 목표를 가진 저는 바보였습니다... 하지만 또 너무 쉽게 앱을 만들면 그건 그거대로 이상한 거죠?? 그러니 힘들더라도 차근차근 풀어나가봅시다. 공부자료)본격적으로 공부하기 전에 자료량이 너무 방대해서 처음에는 리액트 네이티브의 공식 문서를 보면서 공부하려고 했지만 문법이 이해가 안 되니 props나 state가 무엇을 하는진 알아도 어떻게 적어야 될지 모르겠더라고요. 그래서 며칠 낑낑대봤지만 결국 빠른 포기! 책으로 옮기기로 했습니다. Do it!)예전에 파이썬의 데이터 구조 및 알고리즘을 공부할 때 Do it! (이하 두잇)을 사용해 본 적이 있는데 설명도 자세하고 나름 괜찮더군요. 그래서 이번에도 두잇에..
Solution) class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ # Global Variables ROWS, COLS = len(board), len(board[0]) # Helper func that counts neighbors def countNeighbors(r, c): nei = 0 for i in range(r-1, r+2): for j in range(c-1, c+2): # Conditions where board[i][j] DNE if ((i==r and j==c) or i < 0 or j < 0 or..
Solution) class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ # Special flag that indicates if 0th row needs to be zeroes rowZero = False # Setting the flags for r in range(len(matrix)): for c in range(len(matrix[0])): if matrix[r][c] == 0: matrix[0][c] = 0 if r == 0: rowZero = True else: matrix[r][0] = 0 # Convert..
Solution) class Solution: def rotate(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ # Initialization of boundaries left, right = 0, len(matrix)-1 # Select the boundary while left < right: # Rotate the numbers in the boundary for i in range(right-left): top, bottom = left, right temp = matrix[top][left + i] matrix[top][left+i] = matrix[bott..
Solution) class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: # Initialization res = [] left, right = 0, len(matrix[0]) top, bottom = 0, len(matrix) while left < right and top < bottom: # Add top row elements for i in range(left, right): res.append(matrix[top][i]) top += 1 # Add rightmost column elements for i in range(top, bottom): res.append(matrix[i][right-1]) right -..
Solution) class Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: # Dictionaries that have values of sets cols = collections.defaultdict(set) rows = collections.defaultdict(set) squares = collections.defaultdict(set) # Iterating 9 rows for r in range(9): # Iterating 9 columns for c in range(9): # If the current number doesn't exist if board[r][c] == ".": continue # If the curren..