-
-
Notifications
You must be signed in to change notification settings - Fork 358
[essaysir] WEEK 01 Solutions #2648
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
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,12 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public boolean containsDuplicate(int[] nums) { | ||
| Set<Integer> sets = new HashSet<>(); | ||
| for ( int i = 0; i < nums.length; i++){ | ||
| boolean added = sets.add(nums[i]); | ||
| if (!added) return true; | ||
| } | ||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 한 번의 반복으로 이전 상태를 갱신하며, 공간은 상수만 사용한다. 시간은 입력 크기에 비례한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution { | ||
| public int rob(int[] nums) { | ||
| int prev = 0; // dp[i-2]: 두 칸 전까지의 최대 금액 | ||
| int curr = 0; // dp[i-1]: 한 칸 전까지의 최대 금액 | ||
|
|
||
| for (int num : nums) { | ||
| // 현재 집을 털 경우(prev + num) vs 안 털 경우(curr) 중 큰 값 | ||
| int temp = Math.max(curr, prev + num); | ||
| prev = curr; | ||
| curr = temp; | ||
| } | ||
|
|
||
| return curr; | ||
| } | ||
| } |
|
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,23 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| // TC: O(n) | ||
| // SC: O(n) | ||
| public int longestConsecutive(int[] nums) { | ||
| // 연속적인 수의 개수를 구한다. | ||
| Arrays.sort(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. Arrays.sort()의 시간복잡도는 O(nlogn) 이라서, 결국 전체 시간 복잡도는 O(nlogn)으로 봐야할 것 같습니다! |
||
| int maxCount = 0; | ||
| int cur = 1; | ||
| for ( int i = 1 ; i < nums.length; i ++){ | ||
| if (nums[i] == nums[i - 1]) { | ||
| continue; // 중복 건너뛰기 | ||
| } else if (nums[i - 1] + 1 == nums[i]) { | ||
| cur += 1; | ||
| } else { | ||
| cur = 1; | ||
| } | ||
| maxCount = Math.max(maxCount, cur); // 매 스텝마다 갱신 | ||
| } | ||
| return maxCount; | ||
| } | ||
| } | ||
|
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), 힙 연산은 k개에 대해 수행되어 전체 시간은 O(n log k)이다. 공간은 해시맵과 힙에 비례한다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| // TC: O(n log n) | ||
|
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. 시간 복잡도를 수정해야할 것 같습니다.
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. 저도 풀지는 못했는데, 버킷 정렬을 이용해서 최적화할 수 있는 문제더라구요. 버킷 정렬 방법도 고민해보면 좋을 것 같습니다. |
||
| // SC: O(n) | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
| Map<Integer, Integer> count = new HashMap<>(); | ||
| for ( int i = 0; i < nums.length; i++){ | ||
| count.put(nums[i], count.getOrDefault(nums[i],0)+ 1); | ||
| } | ||
|
|
||
| PriorityQueue<Integer> heap = new PriorityQueue<>((a,b) -> count.get(a) - count.get(b)); | ||
|
|
||
| for ( int key : count.keySet()){ | ||
| heap.offer(key); | ||
| if ( heap.size() > k ){ | ||
| heap.poll(); | ||
| } | ||
| } | ||
|
|
||
| int[] result = new int[k]; | ||
| for ( int i = 0; i < k; i++){ | ||
| result[i] = heap.poll(); | ||
| } | ||
| 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 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| // TWO Sum , 순서는 상관이 없음. NC2 를 계산 필요 | ||
| int[] result = new int[2]; | ||
| for ( int i = 0; i < nums.length; i++){ | ||
| for ( int j = i + 1; j < nums.length; j++){ | ||
| if ( target == nums[i] + nums[j]){ | ||
|
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. 정답을 찾았을 경우엔, break를 줘서 불필요하게 for 문을 돌지 않게 해도 좋을 것 같습니다 |
||
| result[0] = i; | ||
| result[1] = j; | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
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에 모든 원소를 저장하며, 중복 검사 시 add 실패 여부로 판단한다. 시간과 공간 모두 입력 크기에 비례한다.
개선 제안: 현재 구현이 적절해 보입니다.