Skip to content

Commit 6398137

Browse files
authored
Merge pull request #1250 from paran22/main
[paran22] WEEK 02 solutions
2 parents a9db161 + 68a7daa commit 6398137

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

3sum/paran22.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
# time complexity: O(n^2)
3+
def threeSum(self, nums: List[int]) -> List[List[int]]:
4+
nums.sort()
5+
answer = set()
6+
7+
for i in range(len(nums) - 2):
8+
if i > 0 and nums[i] == nums[i - 1]:
9+
continue
10+
11+
left, right = i + 1, len(nums) - 1
12+
while left < right:
13+
sum = nums[i] + nums[left] + nums[right]
14+
if sum == 0:
15+
answer.add((nums[i], nums[left], nums[right]))
16+
left += 1
17+
right -= 1
18+
elif sum > 0:
19+
right -= 1
20+
elif sum < 0:
21+
left += 1
22+
23+
return [list(x) for x in answer]
24+

climbing-stairs/paran22.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
# time complexity: O(n)
3+
def climbStairs(self, n: int) -> int:
4+
if n <= 2:
5+
return n
6+
7+
prev1, prev2 = 1, 2
8+
for _ in range(3, n + 1):
9+
current = prev1 + prev2
10+
prev1, prev2 = prev2, current
11+
return prev2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
# time complexity: O(n)
3+
def productExceptSelf(self, nums: List[int]) -> List[int]:
4+
n = len(nums)
5+
answer = [1] * n
6+
7+
prefix = 1
8+
for i in range(n):
9+
answer[i] = prefix
10+
prefix *= nums[i]
11+
12+
suffix = 1
13+
for i in range(n-1, -1, -1):
14+
answer[i] *= suffix
15+
suffix *= nums[i]
16+
17+
return answer
18+

valid-anagram/paran22.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
# time complexity: O(n)
5+
def isAnagram(self, s: str, t: str) -> bool:
6+
return Counter(s) == Counter(t)
7+

0 commit comments

Comments
 (0)