-
-
Notifications
You must be signed in to change notification settings - Fork 357
[seongmin36] WEEK 01 Solutions #2655
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
1912b25
b9fcfd4
10ff2ee
2aa8722
40d8496
29dfd91
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,9 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {boolean} | ||
| */ | ||
| function containsDuplicate(nums) { | ||
| let set_nums = new Set(nums); | ||
|
|
||
| return set_nums.size !== nums.length; | ||
| } |
|
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 방식을 사용한다. 배열 길이만큼 한 번 순회하므로 시간복잡도는 O(n), DP 배열을 저장하는 공간도 배열 크기만큼 필요하므로 공간복잡도는 O(n)이다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| 문제에서 중요한 것은 '비교를 어디서 어떻게 두나'였다. | ||
| [0]번 인덱스에서 시작하거나 [1]번에서 시작하는 두 가지 방식이 있다. | ||
| [0]번과 [1]번 각각의 케이스에서 누적된 합을 저장하고 비교한다. | ||
| 이 값들은 전부 arr 배열의 인덱스에 저장된다. | ||
| 배열의 원소들이 갖는 의미는 원본 배열 nums의 원소들과 arr 배열의 누적된 값의 합 중 큰 값이다. | ||
| 이렇게 설계한 경우에 마지막 인덱스 or 마지막 인덱스 -1의 값 중 큰 수인 max number가 반환된다. | ||
| */ | ||
|
|
||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
|
|
||
| function rob(nums) { | ||
| switch (nums.length) { | ||
| case 1: | ||
| return nums[0]; | ||
| case 2: | ||
| return Math.max(nums[0], nums[1]); | ||
| } | ||
|
|
||
| let arr = []; | ||
| arr[0] = nums[0]; | ||
| arr[1] = nums[1]; | ||
| arr[2] = nums[0] + nums[2]; | ||
|
|
||
| for (let i = 3; i < nums.length; i++) { | ||
| arr[i] = nums[i] + Math.max(arr[i - 2], arr[i - 3]); | ||
| } | ||
|
|
||
| return Math.max(arr[arr.length - 1], arr[arr.length - 2]); | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: Set에 원소를 넣고, 각 원소에 대해 그 이전 숫자가 없는 경우에만 연속 수를 탐색한다. 전체 원소를 한 번씩만 검사하므로 시간복잡도는 O(n), 추가로 저장하는 공간도 O(n)이다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| function longestConsecutive(nums) { | ||
| let set_nums = new Set(nums); | ||
| let max_seq = 0; | ||
|
|
||
| if (set_nums.size === 0) return 0; | ||
|
|
||
| for (let num of set_nums) { | ||
| // num-1이 없는 경우 num이 시작 수 | ||
| if (!set_nums.has(num - 1)) { | ||
| let curNum = num; | ||
| let seq = 1; // 시작점이 된 순간 seq=1 | ||
|
|
||
| while (set_nums.has(curNum + 1)) { | ||
| curNum += 1; | ||
| seq += 1; | ||
| } | ||
| max_seq = Math.max(max_seq, seq); | ||
| } | ||
| } | ||
| return max_seq; | ||
| } |
|
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), 버킷 배열과 결과 저장 공간도 O(n)이다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| reduce()를 사용하여 배열에서 각 원소의 빈도수를 구한다. | ||
| "버킷 정렬"을 사용한 풀이: 원본 배열의 N+1개의 빈 배열을 생성해서 오름차순으로 원소의 빈도수를 넣는다. | ||
| - 장점: 시간복잡도가 빠름 O(N) | ||
| - 단점: 케이스에 따라 빈 메모리가 많이 소모됨 | ||
| 버킷에서 뒤 원소(빈도수가 많은)부터 하나씩 배열에 넣는다. | ||
|
|
||
| 문제: Example 3 빈도수가 동일한 경우는 bucket의 원소가 "[1, 2]"로 출력됨. | ||
| 원인: 이를 1개의 원소로 보았고, length === k 조건에 맞지 않음 → undefined | ||
| + 기존 bucket[i] > 0의 비교는 number일때만 가능하기 때문에 push도 되지않음. | ||
|
|
||
| 해결: bucket[i].length > 0 길이로 비교, slice + map처리로 [1, 2] 예외 케이스 해결 | ||
| */ | ||
|
|
||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} k | ||
| * @return {number[]} | ||
| */ | ||
| function topKFrequent(nums, k) { | ||
| let result = []; | ||
|
|
||
| let frequency = nums.reduce((acc, count) => { | ||
| acc[count] = (acc[count] || 0) + 1; | ||
| return acc; | ||
| }, {}); | ||
|
|
||
| let bucket = Array.from({ length: nums.length + 1 }, () => []); | ||
|
|
||
| for (const [num, count] of Object.entries(frequency)) { | ||
| bucket[count].push(Number(num)); | ||
| } | ||
|
|
||
| for (let i = bucket.length - 1; i >= 0; i--) { | ||
| if (bucket[i].length > 0) { | ||
| result.push(...bucket[i]); | ||
| } | ||
| } | ||
| return result.slice(0, 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), 해시맵 공간도 O(n)이다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| map을 사용해서 구하려고 하는 값(인덱스)을 value로 둔다. → {value: index} 역전 | ||
| map에 {value: index}를 반복하여 저장한다. | ||
| in 연산자로 find_num이 map의 key와 일치하는지 비교한다. | ||
| */ | ||
|
|
||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} target | ||
| * @return {number[]} | ||
| */ | ||
| function twoSum(nums, target) { | ||
| let map = {}; | ||
| let find_num = 0; | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| find_num = target - nums[i]; | ||
|
|
||
| if (find_num in map) { | ||
| return [map[find_num], i]; | ||
| } | ||
|
|
||
| map[nums[i]] = i; | ||
| } | ||
| } |
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 자료구조를 사용하여 배열의 모든 원소를 저장하고, 크기를 비교하여 중복 여부를 판단한다. 이 과정은 배열 크기만큼 한 번 순회하므로 시간복잡도는 O(n), 추가로 저장하는 공간도 배열 크기만큼 필요하므로 공간복잡도는 O(n)이다.
개선 제안: 현재 구현이 적절해 보입니다.