Skip to content

[paran22] WEEK 02 solutions #1250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions 3sum/paran22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
# time complexity: O(n^2)
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
answer = set()

for i in range(len(nums) - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue

left, right = i + 1, len(nums) - 1
while left < right:
sum = nums[i] + nums[left] + nums[right]
if sum == 0:
answer.add((nums[i], nums[left], nums[right]))
left += 1
right -= 1
elif sum > 0:
right -= 1
elif sum < 0:
left += 1

return [list(x) for x in answer]

11 changes: 11 additions & 0 deletions climbing-stairs/paran22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
# time complexity: O(n)
def climbStairs(self, n: int) -> int:
if n <= 2:
return n

prev1, prev2 = 1, 2
for _ in range(3, n + 1):
current = prev1 + prev2
prev1, prev2 = prev2, current
return prev2
18 changes: 18 additions & 0 deletions product-of-array-except-self/paran22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
# time complexity: O(n)
def productExceptSelf(self, nums: List[int]) -> List[int]:
n = len(nums)
answer = [1] * n

prefix = 1
for i in range(n):
answer[i] = prefix
prefix *= nums[i]

suffix = 1
for i in range(n-1, -1, -1):
answer[i] *= suffix
suffix *= nums[i]

return answer

7 changes: 7 additions & 0 deletions valid-anagram/paran22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from collections import Counter

class Solution:
# time complexity: O(n)
def isAnagram(self, s: str, t: str) -> bool:
return Counter(s) == Counter(t)