Skip to content

[hsskey] WEEK 1 solutions #1168

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

Choose a reason for hiding this comment

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

여기는 제가 아직 안풀어서 우선 approve 하고 추후 확인하겠습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
if (nums.length === 0) return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 당연히 length는 0이 아니겠지하고 조건을 안보고 진행했었는데 이부분 까지 꼼꼼하게 생각하신점이 좋습니다👍
(저한테는 다행히 조건에 1 <= nums.length <= 100 가 있어서 통과가 된것같네요.)

if (nums.length === 1) return nums[0];

const dp = new Array(nums.length);

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

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

return dp[nums.length - 1];
};
23 changes: 23 additions & 0 deletions longest-consecutive-sequence/hsskey.js
Copy link
Contributor

Choose a reason for hiding this comment

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

num - 1을 확인해서 시작이 되는 값을 찾는 것이 좋은것같습니다!

발상은 비슷한데 조금 다른 제 방식은 num에서 while을 +1로도 가고, -1 로도 가게했습니다. 그리고 각각 반복할 때, numSet에서 currentNum을 지워주구요!

Copy link
Member Author

Choose a reason for hiding this comment

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

다른 관점에서 생각못해본 부분인데 피드백 감사합니다.
처음 아이디어를 떠올릴땐, 좌표선상에 연속된 sequence 그룹을 펼쳐놓았을때,
해당 그룹들의 이전 이웃값(n-1)이 없을때 sequence 그룹을 만들 수 있겠다 생각해서 표현해놓은 부분입니다.

말씀해주신대로 while문에서 +1이나 -1로도 가게할 수도 있군요👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function(nums) {
const numSet = new Set(nums);
let maxLength = 0;

for (const num of nums) {
if (!numSet.has(num - 1)) {
let currentNum = num;
let currentLength = 1;

while (numSet.has(currentNum + 1)) {
currentNum++;
currentLength++;
}
maxLength = Math.max(maxLength, currentLength);
}
}

return maxLength;
};
22 changes: 22 additions & 0 deletions top-k-frequent-elements/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function(nums, k) {
const map = new Map()

for(let i = 0; i < nums.length; i++) {
if(map.has(nums[i])) {
const prevVal = map.get(nums[i])
map.set(nums[i], prevVal + 1)
} else {
map.set(nums[i], 1)
}
}
Comment on lines +9 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

(개인적인 생각입니다)
아래 코드에서는 Array메서드를 사용하여 명시적으로 해주셔서 여기도 forEach로 사용하는게 조금 더 통일성이 있을 것 같습니다!

Copy link
Member Author

Choose a reason for hiding this comment

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

좋은 피드백 감사합니다 🙇‍♂️
말씀해주신 것처럼 forEach를 쓰면 코드가 더 간결하고 Array 메서드 체이닝과도 일관성이 생길 수 있다는 점 공감합니다.

다만 저는 반복문 처리에서의 미세한 성능 차이를 고려해서 for 루프를 선호하는 편입니다. (주로 실무에서도)
특히 입력 배열이 커질 경우 forEach의 콜백 호출 비용이 누적될 수 있고, break/continue 등의 흐름 제어가 제한되는 점도 고려하고 있습니다.

물론 이번 문제에선 실제 성능 차이는 거의 없겠지만, 개인적으로는 성능 상 이점을 조금이라도 취할 수 있는 구조를 습관화하는 쪽을 택하고 있습니다.

다시 한 번 스타일 일관성에 대한 피드백 감사드려요. 더 고민해볼 수 있는 좋은 계기가 됐습니다!


const result = [...map.entries()].sort((a, b) => b[1] - a[1]).slice(0, k).map((item) => {
return item[0]
})
return result
};
17 changes: 17 additions & 0 deletions two-sum/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
const map = new Map()

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

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