Skip to content

Commit b34fb6d

Browse files
committed
[WHYjun] WEEK 02 solutions - (3/5)
1 parent 1f31fbe commit b34fb6d

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

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

0 commit comments

Comments
 (0)