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
46 changes: 46 additions & 0 deletions contains-duplicate/sujeong-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @param {number[]} nums
* @return {boolean}
*
* 시간복잡도 계산
* 입력 크기(n)가 커질 때 연산 횟수가 n에 비례해서 증가
* => O(n)
*
* 공간복잡도 계산
* indices가 nums에 비례해서 할당
* 상수 i 할당
* => O(n)
*/
var containsDuplicate = function (nums) {
let indices = {};
nums.forEach((num, index) => {
indices[num] = index;
});

for (let i = 0; i < nums.length; i++) {
if (nums[i] in indices && indices[nums[i]] !== i) return true;
}

return false;
};

/**
* @param {number[]} nums
* @return {boolean}
*
* 시간복잡도 계산
* Set의 size속성은 입력 크기와 관계없이 일정한 시간이 걸림
* => O(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

indices.size의 경우 O(1) time이 소요되지만 new Set(nums)에서 내부적으로 nums를 순회하며 원소를 삽입하기 때문에 O(n) time이 소요되므로, 전체 시간복잡도가 O(n)이 될 것 같습니다!

*
* 공간복잡도 계산
* indices가 nums에 비례해서 할당
* 상수 i 할당
* => O(n)
*/
var containsDuplicate = function (nums) {
const indices = new Set(nums);

if (indices.size !== nums.length) return true;

return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

다음과 같이 return 문을 분기 없이 간결하게 나타낼 수도 있을 것 같아요!

return indices.size !== nums.length;

};
28 changes: 28 additions & 0 deletions top-k-frequent-elements/sujeong-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*
* 시간복잡도 계산
* for loop가 O(n)
* reduce가 O(n)
* sort가 O(n log n)
* => O(n log n)
Copy link
Contributor

Choose a reason for hiding this comment

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

현재 코드에서는 for 문 내에 findreduce가 위치하므로 전체 시간 복잡도가 O(n^2)이 될 것으로 보입니다.

*/
var topKFrequent = function (nums, k) {
let result = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

배열이 아닌 hash map을 이용해서 값 별 frequency를 기록한다면 find로 이미 존재하는 키를 O(n)에 찾을 필요도 없고, reduce로 개수를 매번 O(n)에 계산할 필요가 없으므로 시간 복잡도 최적화가 가능할 것 같습니다!

for (let i = 0; i < nums.length; i++) {
if (result.find((el) => el.key == nums[i])) continue;
const count = nums.reduce((cnt, el) => cnt + (nums[i] === el), 0);
result.push({ key: nums[i], value: count });
}

return result
.sort((a, b) => b.value - a.value)
.slice(0, k)
.map((el) => el.key);
};

/**
* Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
*/
52 changes: 52 additions & 0 deletions two-sum/sujeong-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*
* 시간복잡도 계산
* 입력 크기(n)가 커질 때 연산 횟수가 n^2에 비례해서 증가
* => O(n^2)
*
* 공간복잡도 계산
* 상수 i 할당
* 상수 j 할당
* => o(1)
*/
var twoSum = function (nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < nums.length; j++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

j = i + 1로 시작한다면 중복 연산도 제거할 수 있고, for 문 내부에서 i === j 비교할 필요도 없을 것 같습니다!

if (i === j) continue;
if (nums[i] + nums[j] === target) return [i, j];
}
}
};

/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*
* 시간복잡도 계산
* 입력 크기(n)가 커질 때 연산 횟수가 n에 비례해서 증가
* => O(n)
*
* 공간복잡도 계산
* result가 nums에 비례해서 할당
* 상수 i, findNum 할당
* => O(n)
*/
var twoSum = function (nums, target) {
let result = {};

nums.forEach((num, index) => {
result[num] = index;
});

for (let i = 0; i < nums.length; i++) {
const findNum = target - nums[i];

if (findNum in result && result[findNum] !== i) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nums.forEach()result hash map을 미리 만들어놓는 방식이 아닌, for 문을 돌며 한 번에 hash map을 점진적으로 만들어나간다면 result[findNum] !== i를 확인할 필요가 없을 것 같습니다.

return [i, result[findNum]];
}
}
};