반응형 SWE326 [Leetcode][python] 11. Container With Most Water problem: https://leetcode.com/problems/container-with-most-water/ code: brute-force time limit exceeded class Solution: def maxArea(self, height: List[int]) -> int: left = 0 right = len(height)-1 result = 0 while(left int: left = 0 right = len(height)-1 maxWater = 0 while left result maxWater = max(m.. 2022. 3. 10. [Leetcode][python3] 112. Path Sum problem: https://leetcode.com/problems/path-sum/ code : dfs recursive # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: # recursive def _dfs(cur, curSum): # exit condition if cur == None : return .. 2022. 3. 10. [Leetcode][python3] 442. Find All Duplicates in an Array problem: https://leetcode.com/problems/find-all-duplicates-in-an-array/ code: class Solution: def findDuplicates(self, nums: List[int]) -> List[int]: hashmap = dict() result = [] for num in nums: # n times if num in hashmap : #O(1) result.append(num) else: hashmap[num]=True return result # time complexity # O(n) # space complextiy # O(n) code: class Solution: def findDuplicates(self, nums: List[.. 2022. 3. 10. [Leetcode][python3] 69. Sqrt(x) problem: code: # result^2 = x # x= 2 -> output : 1 # x= 3 -> output : 1 # 4 output # 9 output class Solution: def mySqrt(self, x: int) -> int: # edge case if x==0 : return 0 result =1 while result*result 1: approach = int((left+right)/2) if approach*approach >x: right = approach elif approach*approach < x: left = approach elif approach*approach == x: return approach return left time complexity i.. 2022. 3. 10. 이전 1 ··· 12 13 14 15 16 17 18 ··· 82 다음 반응형