-
-
Notifications
You must be signed in to change notification settings - Fork 358
[hellojoyworldz] WEEK 01 solutions #2663
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
a0fa13c
4eb7a4b
3869b3e
0425aa3
149c03f
addbe11
435576e
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,13 @@ | ||
| # - 문제: https://leetcode.com/problems/contains-duplicate/ | ||
| # - 해설: https://www.algodale.com/problems/contains-duplicate/ | ||
|
|
||
| # 217. Contains Duplicate | ||
| # - Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. | ||
|
|
||
| from typing import List | ||
|
|
||
| class Solution: | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
|
|
||
| return len(set(nums)) != len(nums) | ||
|
|
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 피보나치형 DP를 with two variables로 구현해 공간을 최소화했다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # - 문제: https://leetcode.com/problems/house-robber/ | ||
| # - 풀이: https://www.algodale.com/problems/house-robber/ | ||
|
|
||
| from typing import List | ||
|
|
||
|
|
||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| prev1 = 0 | ||
| prev2 = 0 | ||
|
|
||
| for num in nums: | ||
| current = max(prev1, prev2 + num) | ||
| prev2 = prev1 | ||
| prev1 = current | ||
|
|
||
| return prev1 | ||
|
Comment on lines
+7
to
+17
Contributor
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. dp 점화식을 변수로 두니까 공간복잡도가 많이 줄어드네요👍 |
||
|
|
||
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(n log n) |
| Space | O(n) |
피드백: 정렬이 주된 시간 복잡도이며, 중복 제거로 불필요한 비교를 줄인다.
개선 제안: 현재 구현이 명확하고 합리적이다.
풀이 2: longestConsecutive_optimized — Time: O(n) / Space: O(n)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 해시셋을 이용하면 최악의 경우 O(n) 시간에 근접한 성능을 낼 수 있다.
개선 제안: 고려해볼 만한 개선 방향: 해시 기반 구현으로 시간 복잡도 개선 가능.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # - 문제: https://leetcode.com/problems/longest-consecutive-sequence/ | ||
| # - 풀이: https://www.algodale.com/problems/longest-consecutive-sequence/ | ||
|
|
||
| # Current complexity:O(NlogN) | ||
| # Suggested complexity:O(N) | ||
|
|
||
| from typing import List | ||
|
|
||
|
|
||
| class Solution: | ||
| def longestConsecutive(self, nums: List[int]) -> int: | ||
|
|
||
| sorted_nums = sorted(set(nums)) | ||
|
|
||
| max_sequence = 1 | ||
| current_sequence = 1 | ||
|
|
||
| for i in range(1, len(sorted_nums)): | ||
| if sorted_nums[i] - sorted_nums[i - 1] == 1: | ||
| current_sequence += 1 | ||
| max_sequence = max(max_sequence, current_sequence) | ||
| else: | ||
| current_sequence = 1 | ||
|
|
||
| return max(max_sequence, current_sequence) | ||
|
|
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 내장 자료구조를 활용해 구현이 간단하고 명확하다. 개선 제안: 현재 구현이 적합해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # - 문제: https://leetcode.com/problems/top-k-frequent-elements/ | ||
| # - 풀이: https://www.algodale.com/problems/top-k-frequent-elements/ | ||
|
|
||
| from collections import Counter | ||
| from typing import List | ||
|
|
||
|
|
||
| class Solution: | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| counter = Counter(nums) | ||
| return [num for num, count in counter.most_common(k)] | ||
|
|
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 비효율적이므로 해시맵을 사용한 선형 시간 풀이가 일반적이다. 개선 제안: 고려해볼 만한 대안: 해시맵을 이용한 O(n) 풀이로 개선.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # - 문제: https://leetcode.com/problems/two-sum/ | ||
| # - 해설: https://www.algodale.com/problems/two-sum/ | ||
|
|
||
| from typing import List | ||
|
|
||
|
|
||
| class Solution: | ||
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
| for first_index, first_num in enumerate(nums): | ||
| for second_index, second_num in enumerate(nums): | ||
| if first_index == second_index: | ||
| continue | ||
| if first_num + second_num == target: | ||
| return [first_index, second_index] | ||
|
|
||
| return [] | ||
|
Comment on lines
+7
to
+16
Contributor
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문 중첩으로 시간복잡도가 높은데, 해시맵으로 개선하는 것도 좋을 것 같아요👍 |
||
|
|
||
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:
Solution.containsDuplicate— Time: O(n) / Space: O(n)피드백: 집합을 이용해 각 원소를 한 번씩 확인하면서 중복 여부를 확인한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
containsDuplicate_alternative— Time: O(n log n) / Space: O(1)피드백: 정렬으로 중복 여부를 판단하므로 시간 복잡도가 증가한다.
개선 제안: 현재 구현이 더 효율적이므로 대안 필요 없음.