diff --git a/3sum/ppxyn1.py b/3sum/ppxyn1.py new file mode 100644 index 0000000000..8912e7fa36 --- /dev/null +++ b/3sum/ppxyn1.py @@ -0,0 +1,32 @@ +# idea: sorting + two pointer +# The idea was straightforward, but remove the duplication logic was the tricky. + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + n = len(nums) + answer = [] + + for i in range(n): + if i > 0 and nums[i] == nums[i-1]: + continue + + left, right = i+1, n-1 + + while left < right: + s = nums[i] + nums[left] + nums[right] + if s == 0: + answer.append([nums[i], nums[left], nums[right]]) + while left < right and nums[left] == nums[left+1]: + left += 1 + while left < right and nums[right] == nums[right-1]: + right -= 1 + left += 1 + right -= 1 + elif s < 0: + left += 1 + else: + right -= 1 + return answer + + diff --git a/climbing-stairs/ppxyn1.py b/climbing-stairs/ppxyn1.py new file mode 100644 index 0000000000..7515d102c4 --- /dev/null +++ b/climbing-stairs/ppxyn1.py @@ -0,0 +1,20 @@ +# idea: DP +# I'm not always sure how to approach DP problems. I just try working through a few examples step by step and then check that it would be DP. +# If you have any suggestions for how I can come up with DP, I would appreciate your comments :) + +class Solution: + def climbStairs(self, n: int) -> int: + if n <= 2: + return n + dp = [0] * (n+1) + dp[2], dp[3] = 2, 3 + + #for i in range(4, n): error when n=4 + for i in range(4, n+1): + dp[i] = dp[i-1] + dp[i-2] + + return dp[n] + + + + diff --git a/product-of-array-except-self/ppxyn1.py b/product-of-array-except-self/ppxyn1.py new file mode 100644 index 0000000000..f5fb626756 --- /dev/null +++ b/product-of-array-except-self/ppxyn1.py @@ -0,0 +1,17 @@ +# idea: O(n) - There is no multiplication calculate it as O(1) / addition is possible + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + length = len(nums) + answer = [1] * length + + prefix,suffix = 1,1 + for i in range(length): + answer[i] = prefix + prefix *= nums[i] + for j in range(length - 1, -1, -1): + answer[j] *= suffix + suffix *= nums[j] + return answer + + diff --git a/valid-anagram/ppxyn1.py b/valid-anagram/ppxyn1.py new file mode 100644 index 0000000000..77ab26595a --- /dev/null +++ b/valid-anagram/ppxyn1.py @@ -0,0 +1,27 @@ +#idea: dictionary +from collections import Counter + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + s_dic = Counter(sorted(s)) + t_dic = Counter(sorted(t)) + print(s_dic, t_dic) + return s_dic==t_dic + + + +# Trial and Error +''' +When you call sorted() on a dictionary, it only extracts and sorts the keys,and the values are completely ignored. +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + s_dic = Counter(s) + t_dic = Counter(t) + print(s_dic, t_dic) + return sorted(s_dic)==sorted(t_dic) +''' + + + + + diff --git a/validate-binary-search-tree/ppxyn1.py b/validate-binary-search-tree/ppxyn1.py new file mode 100644 index 0000000000..d91bc1346a --- /dev/null +++ b/validate-binary-search-tree/ppxyn1.py @@ -0,0 +1,20 @@ +# idea: - +# # 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, low, high): + if not node: + return True + if not (low < node.val < high): + return False + return dfs(node.left, low, node.val) and dfs(node.right, node.val, high) + return dfs(root, float('-inf'), float('inf')) + + + +