Skip to content

[RiaOh] WEEK 01 solutions #1172

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 5 commits into from
Apr 6, 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
15 changes: 15 additions & 0 deletions contains-duplicate/RiaOh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function (nums) {
let count = [nums[0]];
for (let i = 1; i < nums.length; i++) {
if (count.includes(nums[i])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

includes()는 내부적으로 for문처럼 동작하는 것일까요?
조금 더 개선된 성능을 원하신다면 Set자료구조를 사용해보시는 것도 방법이 될 수 있을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오! 네 감사합니다. set자료구조로 풀어보겠습니다 :)

return true;
} else {
count.push(nums[i]);
}
}
return false;
};
21 changes: 21 additions & 0 deletions house-robber/RiaOh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
if (nums.length <= 1) {
if (nums.length === 0) {
return 0;
} else {
return nums[0];
}
}

const arr = new Array(nums.length + 1);
arr[0] = 0;
arr[1] = nums[0];
for (let i = 2; i < arr.length; i++) {
arr[i] = Math.max(arr[i - 1], arr[i - 2] + nums[i - 1]);
}
return arr[arr.length - 1];
Comment on lines +14 to +20
Copy link
Contributor

Choose a reason for hiding this comment

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

dp를 활용하면 코드가 훨씬 간단해지고 깔끔해지는 것 같네요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

감사합니다 :)

};
23 changes: 23 additions & 0 deletions longest-consecutive-sequence/RiaOh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function (nums) {
if (nums.length === 0) {
return 0;
}
const set = new Set(nums);
const uniquiArr = [...set];
uniquiArr.sort((a, b) => a - b);
const lengthArr = [1];
for (let i = 1; i < uniquiArr.length; i++) {
if (uniquiArr[i - 1] + 1 === uniquiArr[i]) {
const last = lengthArr[lengthArr.length - 1] + 1;
lengthArr.pop();
lengthArr.push(last);
} else {
lengthArr.push(1);
}
}
return Math.max.apply(null, lengthArr);
};
22 changes: 22 additions & 0 deletions top-k-frequent-elements/RiaOh.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 obj = {};
for (let i = 0; i < nums.length; i++) {
if (Object.keys(obj).includes(String(nums[i]))) {
obj[nums[i]] = obj[nums[i]] + 1;
} else {
obj[nums[i]] = 1;
}
}

const keysArr = Object.keys(obj);
const sortedObj = keysArr
.sort((a, b) => obj[b] - obj[a])
.slice(0, k)
.map((num) => Number(num));
return sortedObj;
};
16 changes: 16 additions & 0 deletions two-sum/RiaOh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
let result = [];
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
result = [i, j];
}
}
}
return result;
};