Skip to content

[krokerdile] WEEK 01 Solutions #1150

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 9 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
48 changes: 48 additions & 0 deletions contains-duplicate/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @param {number[]} nums
* @return {boolean}
*/

/**
* 1차 풀이
* - Map을 사용하여 각 숫자의 개수를 세는 방법
*/
var containsDuplicate = function(nums) {
let dict = new Map();

nums.forEach((num)=>{
if(dict.has(num)){
dict.set(num, dict.get(num)+1);
}else{
dict.set(num, 1);
}
})

for (const num of nums) {
if(dict.get(num) >= 2){
return true;
}
}
return false;
};

/**
* 2차풀이
* - Map을 사용하여 각 숫자의 개수를 세는 방법
* - forEach를 사용하지 않고 for of문을 사용하여 반복문을 돌리는 방법
*/
var containsDuplicate = function(nums) {
let dict = new Map();

for (const num of nums) {
if(dict.has(num)){
dict.set(num, dict.get(num)+1);
}else{
dict.set(num, 1);
}
if(dict.get(num) >= 2){
return true;
}
}
return false;
};
18 changes: 18 additions & 0 deletions house-robber/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
if (nums.length === 0) return 0;
if (nums.length === 1) return nums[0];

let 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(dp[i - 1], nums[i] + dp[i - 2]);
}

return dp[nums.length - 1];
};
28 changes: 28 additions & 0 deletions longest-consecutive-sequence/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function(nums) {
if (nums.length === 0) return 0;

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

for (let num of numSet) {
// 연속 수열의 시작점인지 확인
if (!numSet.has(num - 1)) {
let currentNum = num;
let currentLength = 1;

// 연속된 숫자 있는 동안 증가
while (numSet.has(currentNum + 1)) {
currentNum += 1;
currentLength += 1;
}

maxLength = Math.max(maxLength, currentLength);
}
}

return maxLength;
};
26 changes: 26 additions & 0 deletions top-k-frequent-elements/krokerdile.js
Copy link
Contributor

Choose a reason for hiding this comment

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

dict.set(num, (dict.get(num) || 0) + 1);
이렇게 줄여서 써보시면 어떠실까요?

Copy link
Contributor Author

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,26 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function(nums, k) {
let dict = new Map();
let temp = Array.from(new Set([...nums]));
for(const num of nums){
if(dict.has(num)){
dict.set(num, dict.get(num)+1);
}else{
dict.set(num,1);
}
}

temp = temp.sort((a,b)=>{
let aCount = dict.get(a);
let bCount = dict.get(b);

return bCount - aCount;
})

let slice = temp.slice(0,k);
return slice;
};
15 changes: 15 additions & 0 deletions two-sum/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
let dict = new Map();

for(const [index, num] of nums.entries()){
if(dict.has(target-num) && dict.get(target-num) != index){
return [dict.get(target-num),index];
}
dict.set(num, index);
}
};