-
-
Notifications
You must be signed in to change notification settings - Fork 248
[river20s] WEEK 02 solutions #1246
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
9 commits
Select commit
Hold shift + click to select a range
de820b2
feat: Add empty file river20s.py
river20s fb965d5
Remove river20s.py
river20s bc658db
feat: add solution to Valid Anagram #218
river20s 3c54544
feat: add solution to Climbing Stairs
river20s 368b665
feat: linelint를 위한 마지막 줄 개행 문자
river20s ae93e1a
refactor: Improve algorithm performance with dynamic programming
river20s 8eecf23
feat: Add Solution to Product of Array Except Self #239
river20s c7d07ec
feat: linelint를 위한 마지막 줄 개행 문자 추가
river20s 42e889c
refactor: Optimize space usage in productExceptSelf solution
river20s 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,34 @@ | ||
class Solution(object): | ||
def climbStairs(self, n): | ||
""" | ||
:type n: int | ||
:rtype: int | ||
f(n)이 계단을 올라갈 수 있는 방법의 수라면, | ||
f(n) = f(n-1) + f(n-2) | ||
- Time Complexity | ||
재귀로 푸는 경우 지수 시간이 소요되지만, | ||
아래와 같이 다이나믹 프로그래밍으로 푸는 경우 | ||
O(n) 소요 됨 | ||
- Space Complexity | ||
재귀로 푸는 경우, 재귀 호출 스택이 O(n)만큼의 공간을 사용할 수 있음 | ||
아래와 같이 다이나믹 프로그래밍으로 푸는 경우, | ||
리스트 dp에 각 인덱스 별로 결과를 저장하므로 O(n) 사용 됨 | ||
|
||
""" | ||
|
||
# n이 1인 경우 방법은 하나뿐 | ||
if n == 1: | ||
return 1 | ||
|
||
# 길이가 n+1인 리스트 생성 | ||
# 인덱스 0: 시작점(0번째) | ||
# 인덱스 n: n번째 계단 | ||
dp = [0 for i in range(n + 1)] | ||
|
||
dp[1] = 1 # n = 1이면 1을 반환 | ||
dp[2] = 2 # n = 2이면 2를 반환 | ||
|
||
for i in range(3, n + 1): | ||
dp[i] = dp[i - 1] + 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,20 @@ | ||
class Solution(object): | ||
def productExceptSelf(self, nums): | ||
""" | ||
TimeComplexity: O(n) | ||
SpaceComplexity: O(1) | ||
""" | ||
n = len(nums) | ||
answer = [1] * n # 결과를 저장하는 리스트 | ||
|
||
# i번째 요소 왼쪽에 있는 요소들에 대해 prefix product 수행 | ||
for i in range(1, n): | ||
answer[i] = answer[i - 1] * nums[i - 1] | ||
|
||
# 오른쪽에 있는 요소들에 대해 suffix product 수행 | ||
right = 1 # 오른쪽 누적 곱 | ||
for i in range(n - 1, -1, -1): | ||
answer[i] *= right # 현재 인덱스 왼쪽 곱과 오른쪽 곱을 곱함 | ||
right *= nums[i] #오른쪽 누적 곱 업데이트 | ||
|
||
return answer |
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,57 @@ | ||
class Solution(object): | ||
def isAnagram(self, s, t): | ||
""" | ||
:type s: str | ||
:type t: str | ||
:rtype: bool | ||
|
||
s와 t의 길이가 같다는 전제 아래 | ||
s와 t를 각각 딕셔너리에 저장하고 | ||
딕셔너리끼리 비교하여 애너그램 여부를 검사하는 함수. | ||
|
||
- 시간 복잡도: O(n) | ||
- 공간 복잡도: O(n) | ||
알파벳 소문자로 한정할 경우 O(1)로 볼 수도 있지만 | ||
추가 사항인 UNICODE 문자가 입력될 경우를 고려하여 | ||
공간 복잡도를 O(n)으로 계산함. | ||
|
||
""" | ||
# 먼저 s와 t의 길이 비교 | ||
# s와 t가 애너그램이라면 길이가 같음 | ||
if len(s) != len(t): | ||
return False | ||
|
||
# 입력 받은 s의 각 문자를 키, 빈도를 값으로 하는 딕셔너리 | ||
sdict = {} | ||
|
||
# s의 각 문자를 순회하면서 sdict 구축 | ||
# O(n) 시간 소요 | ||
|
||
for char in s: | ||
if char in sdict: | ||
sdict[char] += 1 | ||
else: | ||
sdict[char] = 1 | ||
|
||
# 입력 받은 t의 각 문자를 키, 빈도를 값으로 하는 딕셔너리 | ||
tdict = {} | ||
|
||
# t의 각 문자를 순회하면서 tdict 구축 | ||
# O(n) 시간 소요 | ||
|
||
for char in t: | ||
if char in tdict: | ||
tdict[char] += 1 | ||
else: | ||
tdict[char] = 1 | ||
|
||
# Python은 키의 순서에 상관 없이 딕셔너리끼리 바로 비교 가능 | ||
# sdict와 tdict 비교 후 같으면 True 같지 않으면 False 반환 | ||
# 딕셔너리 안의 키의 수가 k이고 모든 문자가 개별적이라면, | ||
# 시간은 O(k)가 필요 | ||
# 여기서 k는 O(n) 수준이므로 전체 시간 복잡도는 O(n) | ||
|
||
if sdict == tdict: | ||
return True | ||
else: | ||
return False | ||
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.
저는 counter만 사용해서 풀었는데 딕셔너리 접근법도 좋은 것 같습니다! 마지막에 개행문자만 추가해주시면 될 것 같아요!