Skip to content

[reach0908] WEEK 01 solutions #1685

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

Merged
merged 6 commits into from
Jul 27, 2025
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
19 changes: 19 additions & 0 deletions contains-duplicate/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @description
* time complexity: O(n)
* space complexity: O(n)
* 풀이 방법:
* - 주어진 배열을 순회하며 해쉬테이블을 구성하고 해쉬 테이블에 이미 값이 있는 경우 early return 을 통해 중복됨을 반환
* @param {number[]} nums
* @return {boolean}
*/
const containsDuplicate = function (nums) {
const hashTable = new Set();
for (let i = 0; i < nums.length; i++) {
if (hashTable.has(nums[i])) {
return true;
}
hashTable.set(nums[i], i);
}
return false;
};
46 changes: 46 additions & 0 deletions house-robber/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @description
* time complexity: O(2^n)
* space complexity: O(n)
* 풀이 실패
* 풀이 방법: 선택한 경우와 선택하지 않은 경우를 재귀적으로 호출하여 최대값을 반환한다.
* @param {number[]} nums
* @return {number}
*/
const rob = function (nums) {
console.log(nums);
if (nums.length === 0) return 0;
if (nums.length === 1) return nums[0];
if (nums.length === 2) return Math.max(nums[0], nums[1]);

// 선택한 경우
const selected = nums[0] + rob(nums.slice(2));
// 선택하지 않은 경우
const unselected = rob(nums.slice(1));

const max = Math.max(selected, unselected);

return max;
};

/**
* @description
* time complexity: O(n)
* space complexity: O(n)
* runtime: 100ms
* 풀이 방법: 위 풀이가 타임아웃이 남, DP로 풀어야함을 인지 후 풀이 방법 변경
* @param {number[]} nums
* @return {number}
*/
const robSolution2 = function (nums) {
const dp = new Array(nums.length).fill(0);

dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);

for (let i = 2; i < nums.length; i += 1) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
}

return dp[nums.length - 1];
};
68 changes: 68 additions & 0 deletions longest-consecutive-sequence/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @description
* time complexity: O(n log n)
* space complexity: O(n)
* runtime: 57ms
* 풀이 방법: 중복을 제거하고 정렬한 다음에 연속된 숫자의 개수를 카운트하여 최대값을 반환한다.
* @param {number[]} nums
* @return {number}
*/
const longestConsecutive = function (nums) {
if (nums.length === 0) return 0;
const sortedNums = [...new Set(nums)].sort((a, b) => a - b);
Copy link
Contributor

Choose a reason for hiding this comment

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

정렬이 꼭 필요한 문제는 아니라고 생각이 듭니다. 정렬을 쓰면 nlogn으로 시간복잡도가 올라가니깐 정렬 없는 풀이도 한번 생각해보시면 도움이 될 거 같습니다 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

해당 문제 한번 개선해보고 있습니다!


let maxConsecutiveCount = 1;
let currentConsecutiveCount = 1;

for (let i = 1; i < sortedNums.length; i += 1) {
if (sortedNums[i] === sortedNums[i - 1] + 1) {
currentConsecutiveCount += 1;
} else {
currentConsecutiveCount = 1;
}

maxConsecutiveCount = Math.max(
maxConsecutiveCount,
currentConsecutiveCount
);
}

return maxConsecutiveCount;
};

/**
* @description
* time complexity: O(n)
* space complexity: O(n)
* runtime: 36ms
* 풀이 방법: 중복을 제거하고 Set을 사용하여 O(1) 조회 가능하도록 한 다음에 연속된 숫자의 개수를 카운트하여 최대값을 반환한다.
* @param {number[]} nums
* @return {number}
*/
const longestConsecutive2 = function (nums) {
if (nums.length === 0) return 0;

// Set을 사용하여 O(1) 조회 가능
const numSet = new Set(nums);
let maxLength = 0;

for (const num of numSet) {
// 현재 숫자가 연속 수열의 시작점인지 확인
// num-1이 존재하지 않으면 num이 시작점
if (!numSet.has(num - 1)) {
let currentNum = num;
let currentLength = 1;

// 연속된 다음 숫자들이 존재하는 동안 계속 탐색
while (numSet.has(currentNum + 1)) {
currentNum += 1;
currentLength += 1;
}

// 최대 길이 업데이트
maxLength = Math.max(maxLength, currentLength);
}
}

return maxLength;
};
29 changes: 29 additions & 0 deletions top-k-frequent-elements/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @description
* time complexity: O(n log n)
* space complexity: O(n)
* runtime: 3ms
* 풀이 방법:
* 해시맵을 통해 각 숫자의 빈도수를 계산
* 해시맵을 정렬하여 빈도수가 높은 순서대로 정렬
* 정렬된 배열을 k만큼 짤라서 반환
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
const topKFrequent = function (nums, k) {
const hashMap = new Map();

for (const num of nums) {
if (hashMap.has(num)) {
hashMap.set(num, hashMap.get(num) + 1);
} else {
hashMap.set(num, 1);
}
}

return Array.from(hashMap.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, k)
.map(([num]) => num);
};
55 changes: 55 additions & 0 deletions two-sum/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @description
* time complexity: O(n^2)
* space complexity: O(1)
* 풀이 방법:
* 카운터를 통해 반복문 돌기
* 겉으로는 반복문이 한개 같아보이지만 이중반복문이다.
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const twoSumSoluton1 = function (nums, target) {
let left = 0;
let right = 1;
while (left < nums.length - 1) {
const sum = nums[left] + nums[right];
if (sum === target) {
return [left, right];
}

if (right < nums.length - 1) {
right += 1;
} else {
left += 1;
right = left + 1;
}
}
};

/**
* @description
* 다른 사람들의 풀이를 보고 개선한 솔루션
* time complexity: O(n)
* space complexity: O(n)
* 풀이 방법:
* 이전 값들 중에 원하는 값이 있는지만 확인 후 추출, 시간복잡도를 크게 감소시킴
* 하지만 해쉬맵을 만들어야해서 공간복잡도는 O(n)으로 변경
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const twoSumSoluton2 = function (nums, target) {
const hashMap = new Map();

for (let i = 0; i < nums.length; i += 1) {
const calculatedTarget = target - nums[i];
if (hashMap.has(calculatedTarget)) {
return [i, hashMap.get(calculatedTarget)];
}

hashMap.set(nums[i], i);
}

return [];
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 무조건 하나의 유효값이 있다 해서 예외처리를 안했는데, 이렇게 하는 것도 디테일이라고 생각이 드네요. 좋은 것 같습니다 :)

};