Skip to content

[sounmind] WEEK 02 Solutions #1243

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 3 commits into from
Apr 12, 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
60 changes: 0 additions & 60 deletions 3sum/sounmind.js

This file was deleted.

51 changes: 51 additions & 0 deletions 3sum/sounmind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from typing import List


class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
zero_sum_triplets = []
nums.sort() # Sort to handle duplicates and enable two-pointer approach

for first_index in range(len(nums) - 2):
# Skip duplicate values for the first position
if first_index > 0 and nums[first_index] == nums[first_index - 1]:
continue

# Use two-pointer technique to find complementary pairs
second_index = first_index + 1
third_index = len(nums) - 1

while second_index < third_index:
current_sum = nums[first_index] + nums[second_index] + nums[third_index]

if current_sum == 0:
# Found a valid triplet
zero_sum_triplets.append(
[nums[first_index], nums[second_index], nums[third_index]]
)

# Skip duplicates for second and third positions
while (
second_index < third_index
and nums[second_index] == nums[second_index + 1]
):
second_index += 1
while (
second_index < third_index
and nums[third_index] == nums[third_index - 1]
):
third_index -= 1

# Move both pointers inward
# (In a balanced state where sum=0, moving only one pointer would unbalance it)
second_index += 1
third_index -= 1

elif current_sum < 0:
# Current sum is too small, need a larger value
second_index += 1
else:
# Current sum is too large, need a smaller value
third_index -= 1

return zero_sum_triplets
30 changes: 0 additions & 30 deletions climbing-stairs/sounmind.js

This file was deleted.

51 changes: 51 additions & 0 deletions climbing-stairs/sounmind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution:
def climbStairs(self, n: int) -> int:
"""
Climbing Stairs Problem: You are climbing a staircase with n steps.
Each time you can either climb 1 or 2 steps. How many distinct ways can you climb to the top?

This is essentially a Fibonacci sequence problem:
- To reach step n, you can either:
1. Take a single step from step (n-1)
2. Take a double step from step (n-2)
- Therefore, the total ways to reach step n = ways to reach (n-1) + ways to reach (n-2)

Time Complexity: O(n) - We need to calculate each step once
Space Complexity: O(1) - We only store two previous values regardless of input size
"""
# Base cases: There's only 1 way to climb 1 step and 2 ways to climb 2 steps
if n == 1:
return 1
if n == 2:
return 2

# Initialize variables with base cases
# For staircase with 1 step, there's only 1 way to climb
ways_to_reach_n_minus_2 = 1

# For staircase with 2 steps, there are 2 ways to climb (1+1 or 2)
ways_to_reach_n_minus_1 = 2

# Variable to store the current calculation
ways_to_reach_current_step = 0

# Start calculating from step 3 up to step n
for _ in range(3, n + 1):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한 번의 for 루프를 통해 시간복잡도 O(n)와 3개의 정수 변수만 사용하여 공간복잡도 O(1)로 최적화를 하신 풀이를 보고 저도 많은 도움이 되었습니다:)

# To reach current step, we can either:
# 1. Take a single step after reaching step (n-1)
# 2. Take a double step after reaching step (n-2)
# So the total ways = ways to reach (n-1) + ways to reach (n-2)
ways_to_reach_current_step = (
ways_to_reach_n_minus_1 + ways_to_reach_n_minus_2
)

# Shift our window of calculations forward:
# The previous (n-1) step now becomes the (n-2) step for the next iteration
ways_to_reach_n_minus_2 = ways_to_reach_n_minus_1

# The current step calculation becomes the (n-1) step for the next iteration
ways_to_reach_n_minus_1 = ways_to_reach_current_step

# After the final iteration, both ways_to_reach_n_minus_1 and ways_to_reach_current_step
# have the same value (the answer for step n)
return ways_to_reach_n_minus_1 # Could also return ways_to_reach_current_step
33 changes: 0 additions & 33 deletions product-of-array-except-self/sounmind.js

This file was deleted.

21 changes: 21 additions & 0 deletions product-of-array-except-self/sounmind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List


class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
array_length = len(nums)
products_except_self = [1] * array_length

# First pass: multiply by all elements to the left
left_product = 1
for i in range(array_length):
products_except_self[i] = left_product
left_product *= nums[i]

# Second pass: multiply by all elements to the right
right_product = 1
for i in range(array_length - 1, -1, -1):
products_except_self[i] *= right_product
right_product *= nums[i]

return products_except_self