Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions 3sum/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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


20 changes: 20 additions & 0 deletions climbing-stairs/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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]




17 changes: 17 additions & 0 deletions product-of-array-except-self/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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


27 changes: 27 additions & 0 deletions valid-anagram/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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)
'''





20 changes: 20 additions & 0 deletions validate-binary-search-tree/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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'))