Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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,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;
};
31 changes: 31 additions & 0 deletions longest-consecutive-sequence/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @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;
};
29 changes: 29 additions & 0 deletions top-k-frequent-elements/reach0908.js
Copy link
Contributor

Choose a reason for hiding this comment

The 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,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.

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

};