Skip to content

Commit 1d0bf6d

Browse files
authored
Merge pull request #2030 from ppxyn1/main
[ppxyn1] WEEK 02 solutions
2 parents 123611e + 840701f commit 1d0bf6d

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

3sum/ppxyn1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# idea: sorting + two pointer
2+
# The idea was straightforward, but remove the duplication logic was the tricky.
3+
4+
class Solution:
5+
def threeSum(self, nums: List[int]) -> List[List[int]]:
6+
nums.sort()
7+
n = len(nums)
8+
answer = []
9+
10+
for i in range(n):
11+
if i > 0 and nums[i] == nums[i-1]:
12+
continue
13+
14+
left, right = i+1, n-1
15+
16+
while left < right:
17+
s = nums[i] + nums[left] + nums[right]
18+
if s == 0:
19+
answer.append([nums[i], nums[left], nums[right]])
20+
while left < right and nums[left] == nums[left+1]:
21+
left += 1
22+
while left < right and nums[right] == nums[right-1]:
23+
right -= 1
24+
left += 1
25+
right -= 1
26+
elif s < 0:
27+
left += 1
28+
else:
29+
right -= 1
30+
return answer
31+
32+

climbing-stairs/ppxyn1.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# idea: DP
2+
# 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.
3+
# If you have any suggestions for how I can come up with DP, I would appreciate your comments :)
4+
5+
class Solution:
6+
def climbStairs(self, n: int) -> int:
7+
if n <= 2:
8+
return n
9+
dp = [0] * (n+1)
10+
dp[2], dp[3] = 2, 3
11+
12+
#for i in range(4, n): error when n=4
13+
for i in range(4, n+1):
14+
dp[i] = dp[i-1] + dp[i-2]
15+
16+
return dp[n]
17+
18+
19+
20+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# idea: O(n) - There is no multiplication calculate it as O(1) / addition is possible
2+
3+
class Solution:
4+
def productExceptSelf(self, nums: List[int]) -> List[int]:
5+
length = len(nums)
6+
answer = [1] * length
7+
8+
prefix,suffix = 1,1
9+
for i in range(length):
10+
answer[i] = prefix
11+
prefix *= nums[i]
12+
for j in range(length - 1, -1, -1):
13+
answer[j] *= suffix
14+
suffix *= nums[j]
15+
return answer
16+
17+

valid-anagram/ppxyn1.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#idea: dictionary
2+
from collections import Counter
3+
4+
class Solution:
5+
def isAnagram(self, s: str, t: str) -> bool:
6+
s_dic = Counter(sorted(s))
7+
t_dic = Counter(sorted(t))
8+
print(s_dic, t_dic)
9+
return s_dic==t_dic
10+
11+
12+
13+
# Trial and Error
14+
'''
15+
When you call sorted() on a dictionary, it only extracts and sorts the keys,and the values are completely ignored.
16+
class Solution:
17+
def isAnagram(self, s: str, t: str) -> bool:
18+
s_dic = Counter(s)
19+
t_dic = Counter(t)
20+
print(s_dic, t_dic)
21+
return sorted(s_dic)==sorted(t_dic)
22+
'''
23+
24+
25+
26+
27+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# idea: -
2+
# # Definition for a binary tree node.
3+
# class TreeNode:
4+
# def __init__(self, val=0, left=None, right=None):
5+
# self.val = val
6+
# self.left = left
7+
# self.right = right
8+
class Solution:
9+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
10+
def dfs(node, low, high):
11+
if not node:
12+
return True
13+
if not (low < node.val < high):
14+
return False
15+
return dfs(node.left, low, node.val) and dfs(node.right, node.val, high)
16+
return dfs(root, float('-inf'), float('inf'))
17+
18+
19+
20+

0 commit comments

Comments
 (0)