leetcode

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) ..
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..
Solution)class Solution: def minSubArrayLen(self, target: int, nums: List[int]) -> int: # 2 pointers that indicate the window left, right = 0, 0 # window = sum within the window # res = min length of window window, res = 0, len(nums)+1 while right < len(nums) and left = target: # Find the min length of window res = min(res, right-left+1) # Update window window -= nums[left] left += 1 # Update ri..
위대한먼지
'leetcode' 태그의 글 목록 (4 Page)