-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jungsiroo] Week2 #708
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
[jungsiroo] Week2 #708
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a32772e
Two sum Solution
jungsiroo c1e30f2
Product of Array Except Self Solution
jungsiroo 571311f
Delete Week3 solutions(current : week2)
jungsiroo 6bccbb4
valid anagram solution
jungsiroo 858ab7f
climbing stairs solution
jungsiroo d2d4bb1
Merge branch 'DaleStudy:main' into main
jungsiroo 84fe468
3Sum solution
jungsiroo 6ac686f
Decode Ways solution
jungsiroo 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 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,41 @@ | ||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
# Naive Solution | ||
# Tc : O(n^3) / Sc : O(nC_3) | ||
|
||
n = len(nums) | ||
""" | ||
for i in range(n-2): | ||
for j in range(i+1,n-1): | ||
for k in range(j+1, n): | ||
if nums[i]+nums[j]+nums[k] == 0: | ||
ret.add(tuple(sorted([nums[i], nums[j], nums[k]]))) | ||
|
||
ret = [x for x in ret] | ||
return ret | ||
""" | ||
|
||
# Better Solution | ||
# two-sum question with fixed num (traversal in for loop) | ||
# Tc : O(n^2) / Sc : O(n) | ||
ret = [] | ||
nums.sort() | ||
|
||
for i in range(n): | ||
if i>0 and nums[i] == nums[i-1]: | ||
continue | ||
j, k = i+1, n-1 | ||
|
||
while j < k: | ||
sum_ = nums[i] + nums[j] + nums[k] | ||
if sum_ < 0 : j += 1 | ||
elif sum_ > 0 : k -= 1 | ||
else: | ||
ret.append([nums[i], nums[j], nums[k]]) | ||
j += 1 | ||
|
||
while nums[j] == nums[j-1] and j<k: | ||
j += 1 | ||
|
||
return ret | ||
|
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,18 @@ | ||
class Solution: | ||
def climbStairs(self, n: int) -> int: | ||
""" | ||
dynamic programming | ||
dp[0] = 1 | ||
dp[1] = 1 | ||
dp[2] = dp[0] + dp[1] = 2 | ||
dp[3] = dp[1] + dp[2] = 3 | ||
... | ||
|
||
Tc = O(n) / Sc = O(n) | ||
""" | ||
|
||
dp = [1 for _ in range(n+1)] | ||
for i in range(2, n+1): | ||
dp[i] = dp[i-2] + dp[i-1] | ||
return dp[n] | ||
|
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,26 @@ | ||
class Solution: | ||
def numDecodings(self, s: str) -> int: | ||
""" | ||
dp 이용 : 가능한 경우의 수를 구해놓고 그 뒤에 붙을 수 있는가? | ||
ex) 1234 | ||
(1,2) -> (1,2,3) | ||
(12) -> (12,3) | ||
(1) -> (1,23) | ||
|
||
Tc : O(n) / Sc : O(n) | ||
""" | ||
if s[0] == '0': return 0 | ||
|
||
n = len(s) | ||
dp = [0]*(n+1) | ||
dp[0] = dp[1] = 1 | ||
|
||
for i in range(2, n+1): | ||
one = int(s[i-1]) | ||
two = int(s[i-2:i]) | ||
|
||
if 1<=one<=9: dp[i] += dp[i-1] | ||
if 10<=two<=26 : dp[i] += dp[i-2] | ||
|
||
return dp[n] | ||
|
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,30 @@ | ||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
if len(s) != len(t): | ||
return False | ||
|
||
# sort and compare | ||
# Tc : O(nlogn) / Sc : O(n)(Because of python timsort) | ||
""" | ||
s = sorted(s) | ||
t = sorted(t) | ||
for s_char, t_char in zip(s, t): | ||
if s_char != t_char: | ||
return False | ||
return True | ||
""" | ||
|
||
# dictionary to count letters | ||
# Tc : O(n) / Sc : O(n) | ||
letters_cnt = dict() | ||
INF = int(1e6) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오, 보통 이럴 때 |
||
for s_char in s: | ||
letters_cnt[s_char] = letters_cnt.get(s_char,0)+1 | ||
|
||
for t_char in t: | ||
letters_cnt[t_char] = letters_cnt.get(t_char,INF)-1 | ||
if letters_cnt[t_char] < 0: | ||
return False | ||
|
||
return sum(letters_cnt.values()) == 0 | ||
|
Oops, something went wrong.
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.
계단에 오르는 방법의 개수를 굳이 1번째 칸부터 n번째 칸까지 모두 저장할 필요가 있을까요? 현재 답안도 훌륭하지만 아직 마감까지 시간이 좀 남아있으니 혹시 시간되시면 고민해보시면 좋을 것 같습니다 :)