초보

Solution)class Solution: def hIndex(self, citations: List[int]) -> int: # Initialization space = [] for i in range(len(citations)): # Fill empty space if not space: if citations[i] > 0: space.append(citations[i]) else: # If cur elem is bigger than cur h-index if citations[i] > len(space): space.append(citations[i]) # If the smallest elem is smaller than updated h-index if min(space) < len(space)..
Solution)class Solution: def jump(self, nums: List[int]) -> int: # Initialization step, l, r = 0, 0, 0 # while the end of nums is not reached while r < len(nums)-1: farthest = 0 # Find the farthest index that can be jumped from each window for i in range(l, r+1): farthest = max(farthest, nums[i]+i) # Slide window l = r+1 r = farthest # Increment each jump step += 1 return step Problem: LeetCode ..
Solution)class Solution: def canJump(self, nums: List[int]) -> bool: # Initialization goal = len(nums) - 1 # Check if current goal can be reached and update goal for i in range(len(nums)-2, -1, -1): if nums[i] + i >= goal: goal = i # Check if the end can be reached return True if goal == 0 else False Problem: LeetCode Reference: NeetCode Time Complexity: O(n) Space Complexity: O(1)Explanation)I ..
Solution)class Solution: def isPalindrome(self, x: int) -> bool: # Base Case if x < 0: return False # Initialization rev_x = 0 original = x # Making a reversed x while x: rev_x = (rev_x * 10) + (x % 10) x //= 10 # Check if palindrome if rev_x == original: return True return False Problem: LeetCode Time Complexity: O(n) Space Complexity: O(1)Explanation)We'll make a reversed x integer and compare..
Solution) class Solution: def addBinary(self, a: str, b: str) -> str: # Initialization res = "" carry = 0 aIdx, bIdx = len(a)-1, len(b)-1 while aIdx >= 0 or bIdx >= 0: # Adding integers from a and b with carry total = carry if aIdx >= 0: total += int(a[aIdx]) if bIdx >= 0: total += int(b[bIdx]) # Updating carry carry = 0 if total >= 2: total -= 2 carry = 1 # Concatenating the sum to res res += s..
Solution)class Solution: def searchInsert(self, nums: List[int], target: int) -> int: # Two Pointers left, right = 0, len(nums)-1 while left target: # Reached the start of an array if center == 0: return center # Update right pointer right = center-1 else: # Reached the end of an array if center == len(nums)-1: return center+1 # Update left pointer left = center+1 Problem: LeetCode Time Complexi..
Solution) class Solution: def getMinimumDifference(self, root: Optional[TreeNode]) -> int: # Initialization prev, res = None, float("inf") # Depth-First-Search def dfs(node): # Base Case if not node: return # Adjusting the scope nonlocal prev, res # Left Subtree dfs(node.left) # Recursive Call if prev: res = min(res, node.val - prev.val) prev = node # Right Subtree dfs(node.right) dfs(root) retu..
Solution) class Solution: def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]: queue = deque([root]) res = [] while queue: nodeSum = 0 nodeCount = len(queue) for i in range(nodeCount): cur = queue.popleft() nodeSum += cur.val if cur.left: queue.append(cur.left) if cur.right: queue.append(cur.right) res.append(nodeSum/nodeCount) return res Problem: LeetCode Time Complexity: O(n) Sp..
Solution) class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root: return self.invertTree(root.left) self.invertTree(root.right) root.left, root.right = root.right, root.left return root Problem: LeetCode Reference: NeetCode Time Complexity: O(2^n) Space Complexity: O(1) Explanation) This recursive function will invert a current node's children. Base cas..
Solution) class Solution: def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: def helper(root1, root2): if not root1 and not root2: return True if root1 and not root2: return False if not root1 and root2: return False leftRes = helper(root1.left, root2.left) rightRes = helper(root1.right, root2.right) return root1.val == root2.val and leftRes and rightRes return helper(p,..
위대한먼지
'초보' 태그의 글 목록 (6 Page)