개발로 개발

Solution) class RecentCounter: def __init__(self): # This deque will save requests that are called in ping() self.requests = collections.deque() def ping(self, t: int) -> int: # First append the input t self.requests.append(t) # This while loop will pop requests that are not in the range # of the past 3000 milliseconds while not (t-3000
Solution)class Solution: def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]: # preDict is a dict that saves {crs : [prerequiste courses]} preDict = {i:[] for i in range(numCourses)} # Fills up preDict according to the given prerequisites for crs, pre in prerequisites: preDict[crs].append(pre) # visit is a set of vertices that are added to res # path is a set of ver..
Solution)class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: # preMap will save {course : [prerequisites]} # visitSet will save courses that were visited while DFS preMap, visitSet = collections.defaultdict(list), set() # Set up preMap according to the given prerequisites for crs, pre in prerequisites: preMap[crs].append(pre) # Helper function that will ..
Solution) class Solution: def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]: # dict of {key : []} # The list will contain lists of [destination, values needed to reach] adj = collections.defaultdict(list) # Example 1) {a : [[b, 2.0]], b: [[a, 0.5], [c, 3.0]], ...} # a -> b costs 2.0 # b -> c costs 3.0 # Make sure to save a -> b and t..
Solution)class Solution: def cloneGraph(self, node: Optional['Node']) -> Optional['Node']: # Base Case if not node: return # Dict that will save {original nodes : copied nodes} # It's used to check if we've already created a certain node oldToNew = {} # Recursively copy nodes and connect them as neighbors def dfs(node): # Checking if the current node is already copied if node in oldToNew: return..
Solution)class Solution: def solve(self, board: List[List[str]]) -> None: # rows and cols are the number of rows and columns of board # visit is a set of positions of "O" that have been visited before rows, cols, visit = len(board), len(board[0]), set() # Modifies board using BFS method def bfs(r, c): # A list of directions including itself directions = [[0,0], [0,1], [0,-1], [1,0], [-1,0]] # q ..
Solution) class Solution: def numIslands(self, grid: List[List[str]]) -> int: # rows, cols are the actual number of rows and columns of grid # visit will save 1's that were visited when discovering islands # islands will save the number of islands rows, cols = len(grid), len(grid[0]) visit = set() islands = 0 # Helper function bfs() will BFS from the current position # (r, c) to discover the ent..
Solution)class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: # A list that will save values of nodes in an increasing order res = [] # Helper function that will save values to res def dfs(root): # Base Case if not root: return # Append vals in inorder traversal, which gives us # the increasing order dfs(root.left) # Mark res as nonlocal for us to use it # in an inner ..
Solution)class Solution: def isValidBST(self, root: Optional[TreeNode]) -> bool: # Variable prev will save a value of a previous node # Variable check will save a bool of whether this BST is valid prev = -2**31 - 1 check = True # Helper function that compares previous node value # with current ndoe value to validates BST def compare(root): # Marking prev and check as nonlocal to use them # in th..
Solution) class Solution: def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: # Base Case if not root: return [] # zig is a list that saves nodes from right to left # zag is a list that saves nodes from left to right # res is a final list that will have zigzagged nodes' vals zig, zag, res = [], [], [] # Initialization zig.append(root) # Iterate the entire tree while zig or z..
위대한먼지
'개발로 개발' 태그의 글 목록