-
-
Notifications
You must be signed in to change notification settings - Fork 358
[seueooo] WEEK 01 solutions #2664
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,17 @@ | ||
| /** | ||
| 풀이 | ||
| 해시맵에 각 숫자들의 개수를 저장하고, 총 개수와 해시맵의 키 개수를 비교하여 중복 여부를 판단한다. | ||
| */ | ||
| var containsDuplicate = function (nums) { | ||
| let map = {}; | ||
| for (const n of nums) { | ||
| map[n] = map[n] ? map[n] + 1 : 1; | ||
| } | ||
| let count = 0; | ||
| for (const key of Object.keys(map)) { | ||
| count += map[key]; | ||
| } | ||
| if (count === Object.keys(map).length) { | ||
| return false; | ||
| } else return true; | ||
| }; |
|
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 배열을 사용해 각 위치까지의 최적해를 계산한다. 간단하고 직관적이다. 개선 제안: 약간의 공간 최적화가 가능하나 현재 구조도 충분히 명확하다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * 풀이 | ||
| * dp 사용하여 각 집까지 털 수 있는 최대 금액을 계산한다. | ||
| * dp[i]는 0번 집부터 i번 집까지만 고려했을 때 털 수 있는 최대 금액을 나타낸다. | ||
| * 시간 복잡도 - O(n) : 배열을 한 번 순회 | ||
| * 공간 복잡도 - O(n) : dp 배열 생성 | ||
| */ | ||
| var rob = function (nums) { | ||
| const n = nums.length; | ||
| let dp = new Array(n); | ||
|
|
||
| dp[0] = nums[0]; | ||
| dp[1] = Math.max(nums[0], nums[1]); | ||
|
|
||
| for (let i = 2; i < n; i++) { | ||
| dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); | ||
| } | ||
| return dp[n - 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 중복 제거 및 정렬을 통해 연속 수의 길이를 찾는 방식이다. BST LCA와 무관한 문제 풀이처럼 보인다. 개선 제안: 정렬에 의존하므로 최악의 경우 n log n 시간 복잡도이다. 해시맵 기반의 선형 해결도 고려 가능하다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * 풀이 | ||
| * 1. 중복 제거 후 정렬 | ||
| * 2. 연속된 수의 개수를 세어 배열에 저장 | ||
| * 3. 배열을 내림차순으로 정렬 후 첫 번째 요소 반환 | ||
| * 시간 복잡도 - O(n log n) : 중복 제거 O(n) + 정렬 O(n log n) | ||
| * 공간 복잡도 - O(n) : 중복 제거 후 새로운 배열 생성 | ||
| */ | ||
| var longestConsecutive = function (nums) { | ||
| let arr = []; | ||
| let newNums = [...new Set(nums)]; | ||
| newNums.sort((a, b) => a - b); | ||
| let count = 1; | ||
| for (let i = 0; i < newNums.length - 1; i++) { | ||
| if (newNums[i] + 1 === newNums[i + 1]) { | ||
| count++; | ||
| } else { | ||
| arr.push(count); | ||
| count = 1; | ||
| } | ||
| } | ||
| // 마지막 그룹 | ||
| arr.push(count); | ||
| arr.sort((a, b) => b - a); | ||
| return arr[0]; | ||
| }; |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 빈도 계산과 정렬으로 간단히 구현되었다. k가 작으면 효율적이지만 최악의 경우 정렬이 병목이 될 수 있다. 개선 제안: 힙(최대 혹은 최소) 구조를 사용하면 상위 k개를 O(n log k)로 얻을 수 있다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * 풀이 | ||
| * 해시맵을 사용하여 각 숫자의 빈도를 저장하고, 그 값을 기준으로 정렬하여 상위 k개의 요소를 선택한다. | ||
| * 시간 복잡도 - O(n log n) : 해시맵 생성 O(n) + 정렬 O(n log n) | ||
| * 공간 복잡도 - O(n) : 해시맵 저장 공간 | ||
| */ | ||
| var topKFrequent = function (nums, k) { | ||
| let map = {}; | ||
| for (const n of nums) { | ||
| map[n] = map[n] ? map[n] + 1 : 1; | ||
| } | ||
| const sorted = Object.entries(map).sort((a, b) => b[1] - a[1]); | ||
| let answer = []; | ||
| for (let i = 0; i < k; i++) { | ||
| answer.push(Number(sorted[i][0])); | ||
| } | ||
| return answer; | ||
| }; |
|
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,16 @@ | ||
| /** | ||
| 풀이 | ||
| 해시맵으로 값의 존재 여부 확인 -> O(1) | ||
| 배열을 딱 한번 순회하므로 O(n). | ||
| */ | ||
| var twoSum = function (nums, target) { | ||
| let map = new Map(); | ||
| for (let i = 0; i < nums.length; i++) { | ||
| const num = target - nums[i]; | ||
| if (map.has(num)) { | ||
| return [map.get(num), i]; | ||
| } | ||
| map.set(nums[i], i); | ||
| } | ||
| return []; | ||
| }; |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 모든 원소를 한 번씩 방문하고 해시맵에 저장하므로 시간과 공간 복잡도는 선형이다.
개선 제안: 현재 구현이 적절해 보입니다.