Skip to content

Commit 15eb55c

Browse files
authored
Merge pull request #1769 from chrisjune/main
[chrisjune] WEEK 02 Solutions
2 parents 66c05e4 + 6d87b22 commit 15eb55c

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

3sum/chrisjune.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def threeSum(self, nums: List[int]) -> List[List[int]]:
6+
if len(nums) >= 3 and set(nums) == set([0]):
7+
return [[0, 0, 0]]
8+
triplets = set()
9+
for i in range(len(nums) - 2):
10+
seen = set()
11+
for j in range(i + 1, len(nums)):
12+
f = -(nums[i] + nums[j])
13+
if f in seen:
14+
triplet = [nums[i], nums[j], f]
15+
triplets.add(tuple(sorted(triplet)))
16+
seen.add(nums[j])
17+
return list(triplets)

climbing-stairs/chrisjune.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
if n <= 1:
4+
return 1
5+
dp = [0] * n
6+
dp[0] = 1
7+
dp[1] = 2
8+
for i in range(3, n + 1):
9+
dp[i - 1] = dp[i - 2] + dp[i - 3]
10+
return dp[n - 1]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def productExceptSelf(self, nums: List[int]) -> List[int]:
6+
len_n = len(nums)
7+
front_cum = [1] * len_n
8+
back_cum = [1] * len_n
9+
10+
for i in range(1, len_n):
11+
front_cum[i] = front_cum[i - 1] * nums[i - 1]
12+
13+
for j in range(1, len_n):
14+
idx = len_n - j - 1
15+
back_cum[idx] = back_cum[idx + 1] * nums[idx + 1]
16+
17+
final = [front_cum[i] * back_cum[i] for i in range(len_n)]
18+
19+
return final

valid-anagram/chrisjune.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
return sorted(s) == sorted(t)

0 commit comments

Comments
 (0)