-
-
Notifications
You must be signed in to change notification settings - Fork 245
[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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
# 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 |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)로 최적화를 하신 풀이를 보고 저도 많은 도움이 되었습니다:)