-
-
Notifications
You must be signed in to change notification settings - Fork 358
[sonshn] WEEK 01 Solutions #2659
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
fcb31f4
6c69e25
9d7e093
7766ac3
32eb8b6
a1e4dde
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 @@ | ||
| /** | ||
| * HashSet을 사용하여 배열에 중복된 값이 있는지 확인 | ||
| * 시간 복잡도: O(n), 공간 복잡도: O(n) | ||
| */ | ||
| class Solution { | ||
| public boolean containsDuplicate(int[] nums) { | ||
| HashSet<Integer> distinctSet = new HashSet<>(); | ||
|
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. 현재 코드에서는 HashSet 고유 기능보다는 Set 인터페이스에서 제공하는 contains, add 메서드만 사용하고 있는 것 같습니다. 그래서 변수 타입을 HashSet로 두기보다, |
||
|
|
||
| for (int i = 0; i < nums.length; i++) { | ||
| if (distinctSet.contains(nums[i])) return true; | ||
| distinctSet.add(nums[i]); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 위치에서 두 가지 선택(도둑질-현 위치 건너뛰기 vs 건너뛰기)으로 분기하며 중복 재귀 호출이 많아 지수 시간 복잡도가 됩니다. 메모이제이션이 필요합니다. 개선 제안: 고려해볼 만한 대안: 메모이제이션(탐색 중복 제거) 또는 DP bottom-up로 시간 복잡도를 O(n)으로 개선. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * DFS를 이용한 재귀 풀이 | ||
| * | ||
| * 시간 복잡도: O(2^n), 공간 복잡도: O(n) | ||
| */ | ||
| class Solution { | ||
| public int rob(int[] nums) { | ||
| return dfs(nums, 0); | ||
| } | ||
|
|
||
| private int dfs(int[] nums, int start) { | ||
| if (start >= nums.length) { | ||
| return 0; | ||
| } | ||
|
|
||
| return Math.max( | ||
| nums[start] + dfs(nums, start + 2), | ||
| dfs(nums, start + 1) | ||
| ); | ||
| } | ||
| } |
|
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 log n)으로 증가합니다. 중복 제거 및 선형 해법도 가능하지만 현재 방식은 간단합니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import java.util.*; | ||
|
|
||
| /** | ||
| * 주어진 배열에서 연속된 정수의 가장 긴 길이를 찾는 문제 | ||
| * | ||
| * 시간 복잡도: O(n log n), 공간 복잡도: O(1) | ||
| */ | ||
| class Solution { | ||
| public int longestConsecutive(int[] nums) { | ||
| if (nums == null || nums.length == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| Arrays.sort(nums); | ||
|
|
||
| int longest = 0; | ||
| int length = 1; | ||
|
|
||
| for (int i = 0; i < nums.length - 1; i++) { | ||
| if (nums[i] == nums[i + 1]) { | ||
| continue; | ||
| } | ||
|
|
||
| if (nums[i] + 1 == nums[i + 1]) { | ||
| length++; | ||
| } else { | ||
| longest = Math.max(longest, length); | ||
| length = 1; | ||
| } | ||
| } | ||
|
|
||
| longest = Math.max(longest, length); | ||
| return longest; | ||
| } | ||
| } |
|
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) | O(n log m) | ❌ |
| Space | O(n) | O(m) | ❌ |
피드백: 빈도 계산과 정렬로 간단하게 해결하지만, n은 배열 길이, m은 고유 숫자 수입니다. 정렬 대신 힙(heap)을 사용하면 O(n log k)도 가능.
개선 제안: 고려해볼 만한 대안: 해시맵 + 힙을 사용해 상위 k개를 추출하는 방식으로 개선.
풀이 2: Solution.topKFrequent — Time: ❌ O(n log n) → O(n log m) / Space: ❌ O(n) → O(m)
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(n log n) | O(n log m) | ❌ |
| Space | O(n) | O(m) | ❌ |
피드백: 빈도 계산과 정렬로 간단하게 해결하지만, n은 배열 길이, m은 고유 숫자 수입니다. 정렬 대신 힙(heap)을 사용하면 O(n log k)도 가능.
개선 제안: 고려해볼 만한 대안: 해시맵 + 힙을 사용해 상위 k개를 추출하는 방식으로 개선.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
|
|
||
| /** | ||
| * 풀이 | ||
| * 1. 빈도수 계산 | ||
| * - HashMap<Integer, Integer>을 사용하여 각 숫자의 등장 횟수를 저장 | ||
| * - Key는 숫자, Value는 해당 숫자의 빈도수 | ||
| * 2. 고유 숫자 저장 | ||
| * - counter.keySet()을 ArrayList로 변환하여 중복 없이 숫자들만 저장 | ||
| * 3. 빈도수 기준 내림차순 정렬 | ||
| * - list.sort((a, b) -> Integer.compare(counter.get(b), counter.get(a))) | ||
| * - HashMap에 저장된 빈도수를 기준으로 많이 등장한 숫자가 앞에 오도록 정렬 | ||
| * 4. 상위 k개 반환 | ||
| * - 정렬된 리스트의 앞에서부터 k개의 숫자를 result 배열에 담아 반환 | ||
| * | ||
| * 시간 복잡도: O(n log n) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| class Solution { | ||
| public int[] topKFrequent(int[] nums, int 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. 지금 풀어주신 풀이도 충분히 직관적이고 이해하기 좋은 것 같습니다. 예를 들어 PriorityQueue를 활용하면 상위 k개의 후보만 관리할 수 있어서 고유 숫자의 개수가 많고 k가 상대적으로 작을 때 정렬 비용을 줄이는 데 도움이 될 수 있을 것 같습니다. |
||
|
|
||
| Map<Integer, Integer> counter = new HashMap<>(); | ||
|
|
||
| for (int num : nums) { | ||
| counter.put(num, counter.getOrDefault(num, 0) + 1); | ||
| } | ||
|
|
||
| List<Integer> list = new ArrayList<>(counter.keySet()); | ||
|
|
||
| // 빈도수 내림차순 정렬 | ||
| list.sort((a, b) -> Integer.compare(counter.get(b), counter.get(a))); | ||
|
|
||
| int[] result = new int[k]; | ||
|
|
||
| for (int i = 0; i < k; i++) { | ||
| result[i] = list.get(i); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
|
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 @@ | ||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
|
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. 이미 자동리뷰에도 달렸지만, 값을 더 빠르게 조회할 수 있는 자료구조를 활용하면 반복 횟수를 줄일 수 있을 것 같습니다! |
||
| int length = nums.length; | ||
|
|
||
| for (int first = 0; first < length - 1; first++) { | ||
| for (int second = first + 1; second < length; second++) { | ||
| int sum = nums[first] + nums[second]; | ||
|
|
||
| if (sum == target) { | ||
| return new int[]{first, second}; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return new int[0]; | ||
| } | ||
| } | ||
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) → O(n) / Space: ✅ O(n) → O(n)피드백: 집합에 값을 하나씩 삽입하며 중복 여부를 체크하므로 시간은 배열 길이에 비례하고, 추가적인 저장공간은 중복 여부를 판단하기 위해 필요합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.containsDuplicate— Time: ✅ O(n) → O(n) / Space: ✅ O(n) → O(n)피드백: 집합에 값을 하나씩 삽입하며 중복 여부를 체크하므로 시간은 배열 길이에 비례하고, 추가적인 저장공간은 중복 여부를 판단하기 위해 필요합니다.
개선 제안: 현재 구현이 적절해 보입니다.