Skip to content
Open
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
4 changes: 4 additions & 0 deletions contains-duplicate/Cyjin-jani.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const containsDuplicate = function (nums) {
const data = new Set(nums);
return data.size !== nums.length;
};
16 changes: 16 additions & 0 deletions top-k-frequent-elements/Cyjin-jani.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const topKFrequent = function (nums, k) {
const tempArr = Array.from({ length: nums.length }, () => []);
const obj = {};

for (let num of nums) {
obj[num] = (obj[num] || 0) + 1;
}

for (let key in obj) {
const val = obj[key] - 1;
tempArr[val].push(+key);
}

const answer = tempArr.flat();
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요 깔끔한 코드 잘 보았습니다!!

Suggested change
const answer = tempArr.flat();
const answer = [];
for (let i = tempArr.length - 1; i >= 0; i--) {
for (let num of tempArr[i]) {
answer.push(num);
if (answer.length === k) return answer;
}
}
return answer;

flat() 를 사용하는 방식도 좋지만 전체 버킷을 모두 펼치는 대신에 tempArr를 뒤에서부터 순회하면서 answer 에 필요한 k개만 담아 바로 리턴하는 방식도 고려해보시면 좋을 것 같아요.

공간복잡도는 동일한 O(n) 이지만 k가 작은 경우에는 새 배열을 추가로 생성하기보다는 k개의 개수만 모아서 반환하는 방식이 효율적일 수도 있을거 같네요.

return answer.slice(-k);
};
14 changes: 14 additions & 0 deletions two-sum/Cyjin-jani.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const twoSum = function (nums, target) {
let memo = {};

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

if (needed in memo) {
return [memo[needed], i];
}

memo[current] = i;
}
};