Skip to content

Commit 4787677

Browse files
authored
Merge pull request #1243 from sounmind/main
[sounmind] WEEK 02 Solutions
2 parents 995f6cf + 8faf3eb commit 4787677

File tree

6 files changed

+123
-123
lines changed

6 files changed

+123
-123
lines changed

3sum/sounmind.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

3sum/sounmind.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def threeSum(self, nums: List[int]) -> List[List[int]]:
6+
zero_sum_triplets = []
7+
nums.sort() # Sort to handle duplicates and enable two-pointer approach
8+
9+
for first_index in range(len(nums) - 2):
10+
# Skip duplicate values for the first position
11+
if first_index > 0 and nums[first_index] == nums[first_index - 1]:
12+
continue
13+
14+
# Use two-pointer technique to find complementary pairs
15+
second_index = first_index + 1
16+
third_index = len(nums) - 1
17+
18+
while second_index < third_index:
19+
current_sum = nums[first_index] + nums[second_index] + nums[third_index]
20+
21+
if current_sum == 0:
22+
# Found a valid triplet
23+
zero_sum_triplets.append(
24+
[nums[first_index], nums[second_index], nums[third_index]]
25+
)
26+
27+
# Skip duplicates for second and third positions
28+
while (
29+
second_index < third_index
30+
and nums[second_index] == nums[second_index + 1]
31+
):
32+
second_index += 1
33+
while (
34+
second_index < third_index
35+
and nums[third_index] == nums[third_index - 1]
36+
):
37+
third_index -= 1
38+
39+
# Move both pointers inward
40+
# (In a balanced state where sum=0, moving only one pointer would unbalance it)
41+
second_index += 1
42+
third_index -= 1
43+
44+
elif current_sum < 0:
45+
# Current sum is too small, need a larger value
46+
second_index += 1
47+
else:
48+
# Current sum is too large, need a smaller value
49+
third_index -= 1
50+
51+
return zero_sum_triplets

climbing-stairs/sounmind.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

climbing-stairs/sounmind.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
"""
4+
Climbing Stairs Problem: You are climbing a staircase with n steps.
5+
Each time you can either climb 1 or 2 steps. How many distinct ways can you climb to the top?
6+
7+
This is essentially a Fibonacci sequence problem:
8+
- To reach step n, you can either:
9+
1. Take a single step from step (n-1)
10+
2. Take a double step from step (n-2)
11+
- Therefore, the total ways to reach step n = ways to reach (n-1) + ways to reach (n-2)
12+
13+
Time Complexity: O(n) - We need to calculate each step once
14+
Space Complexity: O(1) - We only store two previous values regardless of input size
15+
"""
16+
# Base cases: There's only 1 way to climb 1 step and 2 ways to climb 2 steps
17+
if n == 1:
18+
return 1
19+
if n == 2:
20+
return 2
21+
22+
# Initialize variables with base cases
23+
# For staircase with 1 step, there's only 1 way to climb
24+
ways_to_reach_n_minus_2 = 1
25+
26+
# For staircase with 2 steps, there are 2 ways to climb (1+1 or 2)
27+
ways_to_reach_n_minus_1 = 2
28+
29+
# Variable to store the current calculation
30+
ways_to_reach_current_step = 0
31+
32+
# Start calculating from step 3 up to step n
33+
for _ in range(3, n + 1):
34+
# To reach current step, we can either:
35+
# 1. Take a single step after reaching step (n-1)
36+
# 2. Take a double step after reaching step (n-2)
37+
# So the total ways = ways to reach (n-1) + ways to reach (n-2)
38+
ways_to_reach_current_step = (
39+
ways_to_reach_n_minus_1 + ways_to_reach_n_minus_2
40+
)
41+
42+
# Shift our window of calculations forward:
43+
# The previous (n-1) step now becomes the (n-2) step for the next iteration
44+
ways_to_reach_n_minus_2 = ways_to_reach_n_minus_1
45+
46+
# The current step calculation becomes the (n-1) step for the next iteration
47+
ways_to_reach_n_minus_1 = ways_to_reach_current_step
48+
49+
# After the final iteration, both ways_to_reach_n_minus_1 and ways_to_reach_current_step
50+
# have the same value (the answer for step n)
51+
return ways_to_reach_n_minus_1 # Could also return ways_to_reach_current_step

product-of-array-except-self/sounmind.js

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def productExceptSelf(self, nums: List[int]) -> List[int]:
6+
array_length = len(nums)
7+
products_except_self = [1] * array_length
8+
9+
# First pass: multiply by all elements to the left
10+
left_product = 1
11+
for i in range(array_length):
12+
products_except_self[i] = left_product
13+
left_product *= nums[i]
14+
15+
# Second pass: multiply by all elements to the right
16+
right_product = 1
17+
for i in range(array_length - 1, -1, -1):
18+
products_except_self[i] *= right_product
19+
right_product *= nums[i]
20+
21+
return products_except_self

0 commit comments

Comments
 (0)