diff --git a/3sum/8804who.py b/3sum/8804who.py new file mode 100644 index 0000000000..63208ed231 --- /dev/null +++ b/3sum/8804who.py @@ -0,0 +1,17 @@ +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + temp = {} + answer = {} + nums.sort() + for i in range(len(nums)): + for j in range(i+1, len(nums)): + if -(nums[i]+nums[j]) in temp: + ans = str(nums[i])+','+str(nums[j]) + answer[ans] = 1 + temp[nums[i]] = 1 + temp_ans = [] + for ans in answer.keys(): + s1, s2 = ans.split(',') + temp_ans.append([int(s1), int(s2), -(int(s1)+int(s2))]) + return temp_ans + diff --git a/climbing-stairs/8804who.py b/climbing-stairs/8804who.py new file mode 100644 index 0000000000..b0ebd47619 --- /dev/null +++ b/climbing-stairs/8804who.py @@ -0,0 +1,9 @@ +class Solution: + def climbStairs(self, n: int) -> int: + dp = [1] * (n+1) + + for i in range(2, n+1): + dp[i] = dp[i-1]+dp[i-2] + + return dp[n] + diff --git a/product-of-array-except-self/8804who.py b/product-of-array-except-self/8804who.py new file mode 100644 index 0000000000..3a32c7b765 --- /dev/null +++ b/product-of-array-except-self/8804who.py @@ -0,0 +1,26 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + answer = [] + + product1 = [0] * len(nums) + product2 = [0] * len(nums) + + product1[0] = nums[0] + + for i in range(1, len(nums)): + product1[i] = product1[i-1]*nums[i] + + product2[-1] = nums[-1] + + for i in range(len(nums)-2, -1, -1): + product2[i] = product2[i+1] * nums[i] + + for i in range(len(nums)): + if i == 0: + answer.append(product2[1]) + elif i == len(nums)-1: + answer.append(product1[-2]) + else: + answer.append(product1[i-1]*product2[i+1]) + return answer + diff --git a/valid-anagram/8804who.py b/valid-anagram/8804who.py new file mode 100644 index 0000000000..9e304a8d91 --- /dev/null +++ b/valid-anagram/8804who.py @@ -0,0 +1,5 @@ +from collections import Counter +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + return Counter(s) == Counter(t) + diff --git a/validate-binary-search-tree/8804who.py b/validate-binary-search-tree/8804who.py new file mode 100644 index 0000000000..7e54be79b6 --- /dev/null +++ b/validate-binary-search-tree/8804who.py @@ -0,0 +1,16 @@ +# 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 isValidBST(self, root: Optional[TreeNode]) -> bool: + def dfs(node, min_num, max_num): + if not node: + return True + if max_num <= node.val or node.val <= min_num: + return False + return dfs(node.left, min_num, node.val) and dfs(node.right, node.val, max_num) + return dfs(root, float("-inf"), float("inf")) +