Skip to content

[Yg-cho] WEEK 01 Solutions #1707

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 26, 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
11 changes: 11 additions & 0 deletions contains-duplicate/Yg-cho.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
return new Set(nums).size !== nums.length;
Copy link
Contributor

Choose a reason for hiding this comment

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

전 개수를 세서 2개이상인지 판단했는데 set을 이용한건 되게 좋은 아이디어네요

};

//console.log(containsDuplicate([1, 2, 3, 1])); // true
// console.log(containsDuplicate([1, 2, 3, 4])); // false
// console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true
25 changes: 25 additions & 0 deletions longest-consecutive-sequence/Yg-cho.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function(nums) {
if(nums.length === 0) return 0;

const numSet = new Set(nums);
let longest = 0;

for (const num of numSet) {
if(!numSet.has(num-1)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

자바스크립트는 익숙치 않아서 제가 제대로 이해한진 잘 모르겠습니다
의도가 numSet을 순회하면서 더 작은게 없는지 체크하는 용도로 쓰신 if절이라면
순회 전에 numSet을 sort하면 if문을 사용하지 않아도 될것 같아요. 근데 시간복잡도를 생각해보면 if문이 더 유리할 수 있겠어요!

저도 처음엔 중첩루프로 풀었는데요, dynamic programming을 이용해서 nums와 동일한 크기의 배열을 만들고
배열값이 현재까지의 최대연속된 숫자의 개수로 업데이트하는 방식으로 풀면 단일루프로도 풀 수 있더라고요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

안녕하세요.
sort를 하기 보다, 현재 숫자보다 작은 num-1이 존재하지 않으면 현재 숫자가 이전 보다 작으니 시작점으로 보고, 해당 값부터 증가해 나가면서 연속된 횟수를 찾았습니다.
어떤 순서로 순회하든 모든 시작점을 다 찾게 되고, 순서는 결과에 영향이 없다고 생각하여 sort하진 않았습니다.

리뷰 감사합니다~

let currentNum = num;
let currentLength = 1;

while(numSet.has(currentNum+1)) {
currentNum++;
currentLength++;
}

longest = Math.max(longest, currentLength)
}
}
return longest;
};
19 changes: 19 additions & 0 deletions top-k-frequent-elements/Yg-cho.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function(nums, k) {
//HashMap 선언
const counter = new Map()

//HashMap에 빈도를 value로 저장
for(const num of nums){
counter.set(num,(counter.get(num)|| 0) +1);
}

//keys를 가져와 정렬 후, k만큼 -slice 리턴
return [...counter.keys()]
.sort((a,b) => counter.get(a) - counter.get(b))
.slice(-k)
};
19 changes: 19 additions & 0 deletions two-sum/Yg-cho.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
for(var i = 0; i < nums.length; i++){
let getNum = target - nums[i];
let foundIndex = nums.indexOf(getNum);

// foundIndex가 존재하고(-1이 아니고), 자기 자신이 아닌 경우
if(foundIndex !== -1 && foundIndex !== i) {
return [i, foundIndex];
}
}
};
console.log(twoSum([2, 7, 11, 15], 9)); // [0, 1]
console.log(twoSum([3, 2, 4], 6)); // [1, 2]
console.log(twoSum([3, 3], 6)); // [0, 1]