Repository navigation
[hyedall] WEEK 01 solutions #2926
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
Changes from all commits
76e95ab
9224018
ce76336
66d858c
fc28ee9
2ad7278
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| """ | ||
| https://leetcode.com/problems/contains-duplicate/ | ||
|
|
||
| 시간 복잡도: O(n) | ||
| - set 변환 O(n) | ||
|
|
||
| 공간 복잡도: O(n) | ||
| - set에 저장 | ||
| """ | ||
|
|
||
| class Solution: | ||
| def containsDuplicate(self, nums: list[int]) -> bool: | ||
| if len(set(nums)) == len(nums): | ||
| return False | ||
| else: | ||
| return True |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """ | ||
| https://leetcode.com/problems/house-robber/description/ | ||
| 시간 복잡도: O(n) | ||
| - 모든 배열 한 번씩 순회 O(n) | ||
| - 선택/미선택 값 비교 O(1) | ||
| 공간 복잡도: O(n) | ||
| - 각 인덱스까지의 최대 금액 dp 배열에 저장 | ||
| 점화식: | ||
| - dp[i] = max(nums[i] + dp[i - 2], dp[i - 1]) | ||
| - nums[i] + dp[i - 2]: 현재 집을 터는 경우 | ||
| - dp[i - 1]: 현재 집을 털지 않는 경우 | ||
| """ | ||
|
|
||
| class Solution: | ||
| def rob(self, nums: list[int]) -> int: | ||
| n = len(nums) | ||
| dp = [0] * n | ||
| dp[0] = nums[0] | ||
|
|
||
| for i in range(1, n): | ||
| dp[i] = max(nums[i] + dp[i - 2], dp[i - 1]) | ||
|
|
||
| return dp[-1] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """ | ||
| https://leetcode.com/problems/longest-consecutive-sequence/description/ | ||
|
|
||
| 시간 복잡도: O(n) | ||
| - set 생성 O(n) | ||
| - 각 고유 숫자의 시작점 여부 확인 O(n) | ||
| - 연속 수열 탐색 O(n) | ||
|
|
||
| 공간 복잡도: O(n) | ||
| - 모든 숫자를 set에 저장 | ||
|
|
||
| 풀이 : | ||
| - 시간 복잡도 O(n)를 만족시키기 위해 set으로 연속되는 다음 숫자의 존재 여부만 확인 | ||
| - 이전 숫자가 있는지 확인 후 시작점에서만 순회하여 중복 탐색 방지 | ||
| """ | ||
|
|
||
| class Solution: | ||
| def longestConsecutive(self, nums: List[int]) -> int: | ||
| num_set = set(nums) | ||
| answer = 0 | ||
|
|
||
| for num in num_set: | ||
| # 시작점인지 확인 | ||
| if num - 1 not in num_set: | ||
| current = num | ||
| length = 1 | ||
|
|
||
| # 연속 숫자 탐색 | ||
| while current + 1 in num_set: | ||
| current += 1 | ||
| length += 1 | ||
|
|
||
| answer = max(answer, length) | ||
|
|
||
| return answer | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| """ | ||
| https://leetcode.com/problems/top-k-frequent-elements/description/ | ||
| 시간 복잡도: O(n log n) | ||
| - 모든 숫자의 빈도 계산 O(n) | ||
| - 빈도가 높은 k개 선택 O(n log k) | ||
| - 결과 리스트 생성 O(k) | ||
| 공간 복잡도: O(n) | ||
| - 고유 숫자별 빈도 저장 O(n) | ||
| - 정답 k개 저장 O(k) | ||
| """ | ||
|
|
||
| from collections import Counter | ||
|
|
||
| class Solution: | ||
| def topKFrequent(self, nums: list[int], k: int) -> list[int]: | ||
| counts = Counter(nums) | ||
| answer = [n for n, f in counts.most_common(k)] | ||
|
Comment on lines
+18
to
+19
Collaborator
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. 내장 함수를 쓰지 않고 직접 구현해보시면 좋을 거 같아요.
Contributor
Author
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. 리뷰 남겨주셔서 감사합니다 수정해보겠습니다 ! |
||
|
|
||
| return answer | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| """ | ||
| https://leetcode.com/problems/two-sum/ | ||
| 시간 복잡도: O(n²) | ||
| - 첫 번째 반복문에서 최대 n번 순회 | ||
| - 각 숫자마다 뒤의 숫자들을 다시 순회 | ||
| - 확인하는 숫자 쌍의 개수는 약 n(n - 1) / 2개 | ||
| 공간 복잡도: O(1) | ||
| - 입력 크기와 관계없이 i, j 등의 고정된 변수만 사용 | ||
| """ | ||
|
|
||
| class Solution: | ||
| def twoSum(self, nums: list[int], target: int) -> list[int]: | ||
| n = len(nums) | ||
|
|
||
| for i in range(n): | ||
| for j in range(i+1, n): | ||
| if nums[i] + nums[j] == target: | ||
| return [i, j] | ||
|
Comment on lines
+18
to
+21
Collaborator
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. 단일 포문으로도 해결이 가능해서 시간 복잡도를 개선해보시면 좋을 거 같아요 |
||
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.
의견 주셔서 감사합니다 ! 반영했습니다