Skip to content

Commit d17ca08

Browse files
authored
Merge pull request #2026 from WHYjun/week2
[WHYjun] WEEK 02 solutions
2 parents b81ea68 + 1cba52b commit d17ca08

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

3sum/WHYjun.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
answer = []
4+
sortedNums = sorted(nums)
5+
6+
for i in range(len(sortedNums)):
7+
# skip if same to avoid dup
8+
if i > 0 and sortedNums[i] == sortedNums[i-1]:
9+
continue
10+
11+
# use two pointers
12+
j = i + 1
13+
k = len(sortedNums) - 1
14+
15+
while j < k:
16+
total = sortedNums[i] + sortedNums[j] + sortedNums[k]
17+
18+
if total == 0:
19+
answer.append([sortedNums[i], sortedNums[j], sortedNums[k]])
20+
j += 1
21+
# skip if same to avoid dup
22+
while sortedNums[j] == sortedNums[j-1] and j < k:
23+
j += 1
24+
elif total < 0:
25+
j += 1
26+
else:
27+
k -= 1
28+
29+
return answer

climbing-stairs/WHYjun.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
dp = {}
4+
return self.climbStairsRecursively(n, dp)
5+
6+
def climbStairsRecursively(self, n: int, dp: Dict[int, int]):
7+
if n == 1:
8+
dp[1] = 1
9+
return dp[1]
10+
if n == 2:
11+
dp[2] = 2
12+
return dp[2]
13+
14+
if n - 2 not in dp:
15+
dp[n - 2] = self.climbStairsRecursively(n - 2, dp)
16+
17+
if n - 1 not in dp:
18+
dp[n - 1] = self.climbStairsRecursively(n - 1, dp)
19+
20+
return dp[n - 2] + dp[n - 1]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
answer = [1] * len(nums)
4+
for i in range(len(nums)):
5+
if i == 0:
6+
continue
7+
answer[i] = answer[i-1] * nums[i-1]
8+
9+
for j in range(len(nums)-1, -1, -1):
10+
if j == len(nums) - 1:
11+
product = 1
12+
answer[j] *= product
13+
product *= nums[j]
14+
15+
return answer
16+
17+
def productExceptSelfTwoArrays(self, nums: List[int]) -> List[int]:
18+
leftProduct = [1] * len(nums)
19+
rightProduct = [1] * len(nums)
20+
21+
for i in range(len(nums)):
22+
if i == 0:
23+
continue
24+
leftProduct[i] = leftProduct[i-1] * nums[i-1]
25+
26+
for j in range(len(nums)-1, -1, -1):
27+
if j == len(nums) - 1:
28+
continue
29+
rightProduct[j] = rightProduct[j+1] * nums[j+1]
30+
31+
for i in range(len(nums)):
32+
leftProduct[i] *= rightProduct[i]
33+
34+
return leftProduct
35+

valid-anagram/WHYjun.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
if len(s) != len(t):
4+
return False
5+
6+
sCount = {}
7+
tCount = {}
8+
9+
for i in range(len(s)):
10+
sCount[s[i]] = sCount.get(s[i], 0) + 1
11+
tCount[t[i]] = tCount.get(t[i], 0) + 1
12+
13+
for char in sCount.keys():
14+
if sCount.get(char, -1) != tCount.get(char, -1):
15+
return False
16+
17+
return True
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
9+
# https://stackoverflow.com/a/37300370
10+
return self.isValid(root, float('-inf'), float('inf'))
11+
12+
def isValid(self, node: Optional[TreeNode], low: float, high: float):
13+
if node is None:
14+
return True
15+
if node.val <= low or node.val >= high:
16+
return False
17+
return self.isValid(node.left, low, node.val) and self.isValid(node.right, node.val, high)

0 commit comments

Comments
 (0)