Skip to content

Commit a42ab79

Browse files
authored
Merge pull request #1249 from i-mprovising/main
[i-mprovising] Week 02 solutions
2 parents a34bd48 + 6cd32b5 commit a42ab79

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

climbing-stairs/i-mprovising.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Time complexity O(n)
3+
Space complexity O(n)
4+
5+
Dynamic programming
6+
"""
7+
8+
class Solution:
9+
def climbStairs(self, n: int) -> int:
10+
dp = [0, 1, 2] # distinct ways to reach i steps
11+
if n <= 2:
12+
return dp[n]
13+
14+
for i in range(3, n+1):
15+
dp.append(dp[i-1] + dp[i-2])
16+
return dp[n]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Time complexity O(n)
3+
Space complexity O(n)
4+
5+
Prefix sum
6+
"""
7+
8+
class Solution:
9+
def productExceptSelf(self, nums: List[int]) -> List[int]:
10+
products = [1]
11+
reverse_products = [1]
12+
13+
tmp = 1
14+
for n in nums[:-1]:
15+
tmp *= n
16+
products.append(tmp)
17+
tmp = 1
18+
for n in nums[::-1][:-1]:
19+
tmp *= n
20+
reverse_products.append(tmp)
21+
22+
answer = [
23+
products[i] * reverse_products[-i-1]
24+
for i in range(len(nums))
25+
]
26+
return answer

valid-anagram/i-mprovising.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Time complexity O(n)
3+
Space complexity O(n)
4+
"""
5+
from collections import defaultdict
6+
7+
class Solution:
8+
def isAnagram(self, s: str, t: str) -> bool:
9+
s_cnt = defaultdict(int)
10+
t_cnt = defaultdict(int)
11+
for char in s:
12+
s_cnt[char] += 1
13+
for char in t:
14+
t_cnt[char] += 1
15+
if s_cnt != t_cnt:
16+
return False
17+
return True

0 commit comments

Comments
 (0)