Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions contains-duplicate/seongmin36.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set
  • 설명: 이 코드는 Set 자료구조를 이용해 중복 여부를 빠르게 판단하므로 해시 맵 또는 해시 셋 패턴에 속합니다. 중복 체크를 위해 집합의 크기와 배열 길이 비교를 사용합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: Set 자료구조를 사용하여 배열의 모든 원소를 저장하고, 크기를 비교하여 중복 여부를 판단한다. 이 과정은 배열 크기만큼 한 번 순회하므로 시간복잡도는 O(n), 추가로 저장하는 공간도 배열 크기만큼 필요하므로 공간복잡도는 O(n)이다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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;
}
33 changes: 33 additions & 0 deletions house-robber/seongmin36.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 이전 상태의 최적 해를 이용하여 현재 상태를 계산하는 방식으로, 최적 부분 구조를 갖는 문제 해결에 적합한 DP 패턴을 사용합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 배열을 순회하며 각 위치까지의 최대 금액을 저장하는 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]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전 그냥 Math.max(...arr) 로 했는데, 마지막 두 개의 집만 비교해도 되겠군요. 👍

}
25 changes: 25 additions & 0 deletions longest-consecutive-sequence/seongmin36.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set, Union Find
  • 설명: 이 코드는 숫자 집합을 이용해 연속된 수열을 찾으며, Hash Set을 활용하여 빠른 탐색을 수행합니다. Union Find는 사용되지 않지만, 집합 기반으로 연속성을 체크하는 방식이 유사합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 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;
}
40 changes: 40 additions & 0 deletions top-k-frequent-elements/seongmin36.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set, Bucket Sort
  • 설명: 이 코드는 숫자 빈도수 계산에 해시 맵을 사용하고, 버킷 정렬 방식을 통해 빈도별 그룹화 후 결과를 도출하는 방식입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 배열의 원소 빈도수 계산 후, 버킷 정렬을 통해 빈도별 원소를 저장한다. 이후 빈도 높은 순서대로 결과를 추출하므로 시간복잡도는 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);
}
25 changes: 25 additions & 0 deletions two-sum/seongmin36.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set
  • 설명: 이 코드는 해시 맵을 사용하여 각 숫자와 그 인덱스를 저장하고, 목표값과의 차이를 찾는 방식으로 해결한다. 빠른 검색을 위해 해시 맵을 활용하는 특징이 있다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 배열을 한 번 순회하며 각 원소를 해시맵에 저장하고, 대상 원소와 차이값이 해시맵에 존재하는지 검사한다. 이 과정은 배열 크기만큼 수행되므로 시간복잡도는 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;
}
}
Loading