-
Notifications
You must be signed in to change notification settings - Fork 126
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
[thispath98] Week 3 #778
Merged
Merged
[thispath98] Week 3 #778
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cd70330
feat: Add reverse-bits solutions
thispath98 3e3a08e
feat: Add two-sum solutions
thispath98 ba0ac0a
feat: Add product-of-array-except-self solutions
thispath98 1205cd0
feat: Add Combination Sum solutions
thispath98 25f4d4c
feat: Add Maximum Subarray solutions
thispath98 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 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,35 @@ | ||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
""" | ||
Intuition: | ||
i번째 인덱스의 값을 계산하기 위해서는 | ||
0 ~ i-1 까지의 값과 i+1 ~ N 까지의 값을 모두 곱해야 한다. | ||
이의 누적곱을 저장하여, 계산한다. | ||
|
||
Time Complexity: | ||
O(N): | ||
리스트를 1번 순회하며 답을 찾으므로, | ||
O(N)의 시간복잡도가 소요된다. | ||
|
||
Space Complexity: | ||
O(N): | ||
forward 배열과 backward 배열에 N개의 원소를 저장하므로 | ||
O(N)의 공간복잡도가 소요된다. | ||
|
||
Key takeaway: | ||
스캔하여 값을 저장해두는 방식을 숙지하자. | ||
""" | ||
for_val = 1 | ||
back_val = 1 | ||
forward = [] | ||
backward = [] | ||
for i in range(len(nums)): | ||
forward.append(for_val) | ||
backward.append(back_val) | ||
|
||
for_val *= nums[i] | ||
back_val *= nums[-(i + 1)] | ||
backward = backward[::-1] | ||
|
||
answer = [forward[i] * backward[i] for i in range(len(nums))] | ||
return answer |
This file contains 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 reverseBits(self, n: int) -> int: | ||
""" | ||
Intuition: | ||
비트를 역순으로 순회한다. | ||
answer에는 최대값(2^31)부터 최소값(2^0)으로 감소하는 | ||
방식으로 업데이트한다. | ||
|
||
Time Complexity: | ||
O(N): | ||
n을 1번 순회하며 답을 찾으므로, | ||
O(N)의 시간복잡도가 소요된다. | ||
|
||
Space Complexity: | ||
O(1): | ||
answer에 값을 업데이트 하므로, 상수의 | ||
공간복잡도가 소요된다. | ||
|
||
Key takeaway: | ||
숫자를 binary string으로 만드는 bin() 메소드를 | ||
알게 되었다. | ||
""" | ||
answer = 0 | ||
for i, bit in enumerate(bin(n)[2:][::-1]): | ||
answer += int(bit) * 2 ** (31 - i) | ||
return answer |
This file contains 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,23 @@ | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
""" | ||
Intuition: | ||
기존에 풀었던 3sum 문제와 유사하게, | ||
해시에 현재 숫자와 더해서 target이 되는 값을 찾는다. | ||
만약 없을 경우, 해시에 현재 값과 인덱스를 저장한다. | ||
|
||
Time Complexity: | ||
O(N): | ||
해시는 접근하는 데에 O(1)이 소요되고, | ||
총 N번 반복해야 하므로 시간복잡도는 O(N)이다. | ||
|
||
Space Complexity: | ||
O(N): | ||
최악의 경우 해시에 N개의 숫자와 인덱스를 저장해야 한다. | ||
""" | ||
complement_dict = {} | ||
for i, num in enumerate(nums): | ||
if target - num in complement_dict: | ||
return [complement_dict[target - num], i] | ||
else: | ||
complement_dict[num] = i |
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.
무시하셔도 되는 제안입니다 :) 이참에 비트연산자를 이용한 풀이도 도전해보시는 건 어떨까요?
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.
넵 한번 도전해보겠습니다 ㅎㅎ 무시해도 된다는 것은 어떤 의미일까요?
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.
본 피드백을 반영하지 않으셔도 PR 승인에는 영향이 없다라는 말씀입니다. "자율"적인 스터디 문화 장려를 위해서 @thispath98 님께 선택 권한을 드리려는 것 같습니다. (참고: 스터디 원칙)