-
-
Notifications
You must be signed in to change notification settings - Fork 195
[river20s] WEEK 01 solutions #1174
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
0740ac6
feat: add solution to contains duplicate(#217)
river20s ee22121
feat: 개행문자 추가
river20s 6c270a7
fix: linelint를 위한 마지막 줄 개행문자 정리
river20s 80b737e
fix: linelint 에러 해결을 위한 개행 문자 추가
river20s 7c7630a
```
river20s 266da66
feat: Add Two Sum Solution #219
river20s 3265bef
feat: add solution to Top-k frequent elements(#237)
river20s 9efb854
feat: add solution to Longest Consecutive Sequence #240
river20s cd41368
feat: add solution to House Robber #264
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,39 @@ | ||
class Solution(object): | ||
|
||
def containsDuplicate(self, nums): | ||
|
||
""" | ||
|
||
:type nums: List[int] | ||
|
||
:rtype: bool | ||
|
||
nums 리스트에 중복 값이 나오면 false, | ||
|
||
리스트에 중복 값이 없으면 true를 반환한다. | ||
|
||
- Time Complexity: O(n) | ||
|
||
* 전체 배열을 한 번씩 순회하면서 중복 여부를 확인한다. | ||
|
||
- Space Complexity: O(n) | ||
|
||
* Python에서 set()은 배열의 각 원소를 해시 테이블에 저장한다. | ||
|
||
배열에 중복된 요소가 전혀 없다면 모든 요소(n개)를 set에 저장하게 되므로 | ||
|
||
공간 복잡도는 O(n)이 된다. | ||
|
||
""" | ||
|
||
seen = set() # 중복 여부 확인하는 set 객체 | ||
|
||
for item in nums: | ||
|
||
if item in seen: | ||
|
||
return True | ||
|
||
seen.add(item) | ||
|
||
return False |
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,48 @@ | ||
class Solution(object): | ||
def rob(self, nums): | ||
""" | ||
# 초기 풀이 | ||
rob_house_even = 0 # 짝수 집들을 터는 경우 금액의 합 | ||
rob_house_odd = 0 # 홀수 집들을 터는 경우 금액의 합 | ||
for index, value in enumerate(nums): | ||
if index % 2 == 0: | ||
rob_house_even += value | ||
else: | ||
rob_house_odd += value | ||
|
||
return max(rob_house_even, rob_house_odd) | ||
# 실패: | ||
# 단순히 짝수와 홀수를 구분하는 것만으로 | ||
# 최적 해를 구할 수 없음 | ||
|
||
=========================================== | ||
|
||
동적 프로그래밍을 활용해서 문제를 해결 | ||
각각의 최적 해를 누적하여 마지막까지의 최적 해를 구해야 함 | ||
|
||
""" | ||
if not nums: # 빈 리스트가 주어질 경우, 0을 반환 | ||
return 0 | ||
if len(nums) == 1: # 요소가 하나인 경우, 그 값을 반환 | ||
return nums[0] | ||
|
||
# prev2: i-2번째까지 고려 했을 때의 최대 금액 | ||
# prev1: i-1번째까지 고려 했을 때의 최대 금액 | ||
# 초기값 설정: | ||
# - 첫 번째 집(인덱스 0)만 고려한 경우 nums[0] | ||
# - 두 번째 집(인덱스 1)까지 고려한 경우 첫 번째 집과 두 번째 집 중 큰 값 | ||
prev2, prev1 = nums[0], max(nums[0], nums[1]) | ||
|
||
for i in range(2, len(nums)): | ||
# 두 선택지 중 더 금액이 큰 경우를 계산 | ||
# 1. i번째 집을 털지 않을 때: 이전까지의 최대 금액 prev1 | ||
# 2. i번째 집을 털 때: i-1번째 집은 털 수 없음 | ||
# i-2번째 집까지의 최대 금액 prev2에 현재 집의 금액 nums[i]를 더함 | ||
current = max(prev1, prev2 + nums[i]) | ||
|
||
# 이전 단계의 prev1은 다음 단계에서 prev2가, | ||
# current 값은 새로운 prev1이 됨 | ||
prev2, prev1 = prev1, current | ||
|
||
# 마지막 집까지의 최대 금액인 prev1 반환 | ||
return prev1 |
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,31 @@ | ||
class Solution(object): | ||
def longestConsecutive(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
>> nums에서 연속된 수열 중 가장 긴 수열의 길이를 반환합니다. | ||
(전체 풀이 과정은 다음 링크를 참고하시면 됩니다: https://blog.naver.com/kanosekai/223823200575) | ||
- Time Complexity: O(n) | ||
nums를 set으로 변환하는 데 O(n) 시간이 소요됩니다. | ||
- Space Complexity: O(n) | ||
모든 원소가 고유한 경우 전부 set에 저장하므로 | ||
O(n)의 공간을 추가로 사용할 수 있습니다. | ||
""" | ||
num_set = set(nums) | ||
longest = 0 | ||
|
||
for num in num_set: | ||
# num이 연속 구간의 시작점인지 판단 | ||
if num - 1 not in num_set: | ||
current = num | ||
current_streak = 1 | ||
|
||
# 현재 숫자에서 연속된 값들이 set에 있는지 확인하며 수열 확장 | ||
while current + 1 in num_set: | ||
current += 1 | ||
current_streak += 1 | ||
|
||
# 가장 긴 수열의 길이를 반환 | ||
longest = max(longest, current_streak) | ||
|
||
return longest |
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 topKFrequent(self, nums, k): | ||
""" | ||
주어진 정수 리스트에서 각 숫자의 등장 빈도를 계산한 후, | ||
빈도수가 높은 순서대로 상위 k개의 숫자를 반환합니다. | ||
|
||
:param nums: 정수로 이루어진 리스트 | ||
:param k: 반환할 상위 빈도 숫자의 개수 | ||
:return: 빈도수가 높은 순서대로 정렬된 상위 k개 숫자의 리스트 | ||
""" | ||
freq = {} # 각 숫자 등장 횟수를 저장하는 딕셔너리 | ||
# 리스트의 각 숫자 등장 횟수를 누적 저장 | ||
for num in nums: | ||
# num이 freq에 있다면 기존 값에 1을 더하고, 없으면 0에서 1을 더함 | ||
freq[num] = freq.get(num, 0) + 1 | ||
|
||
# freq의 키들을 등장 횟수 기준 내림차순 정렬 | ||
sorted_list = sorted(freq, key=freq.get, reverse=True) | ||
# 정렬된 리스트에서 top-k개 숫자를 반환 | ||
return sorted_list[:k] |
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,16 @@ | ||
class Solution(object): | ||
def twoSum(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: List[int] | ||
""" | ||
seen = {} | ||
for i, num in enumerate(nums): | ||
comp = target - num | ||
if comp in seen: | ||
return seen[comp], i | ||
if num not in seen: | ||
seen[num] = i | ||
return None | ||
|
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.
안녕하세요. 사실 딱히 리뷰해드릴 것도 없고, set을 쓰는 발상은 배워갑니다.
그래도 먼가 도움을 드려야할것 같아서 쥐어짜서 남깁니다.
저장되는 요소의 개수가 n에 비례하여 선형적으로 증가하므로 공간 복잡도는 O(n)이 된다.
이게 좀 더 정확한 표현이지 않을까 싶네요.