Skip to content

[uraflower] WEEK 01 solutions #1128

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 7 commits into from
Apr 4, 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
18 changes: 18 additions & 0 deletions contains-duplicate/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 입력 배열 내 값 중복 여부를 반환하는 함수
* @param {number[]} nums
* @return {boolean}
*/
const containsDuplicate = function (nums) {
const set = new Set();

for (const num of nums) {
if (set.has(num)) return true;
else set.add(num);
}

return false;
};

// 시간복잡도: O(n)
// 공간복잡도: O(n)
17 changes: 17 additions & 0 deletions house-robber/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 주어진 배열에서 인접하지 않은 숫자들의 최대 합을 반환하는 함수
* @param {number[]} nums
* @return {number}
*/
const rob = function(nums) {
const dp = [];

nums.forEach((num, idx) => {
dp[idx] = Math.max((dp[idx - 2] || 0) + num, dp[idx - 1] || 0);
});

return dp[dp.length - 1];
};

// 시간복잡도: O(n);
// 공간복잡도: O(n);
32 changes: 32 additions & 0 deletions longest-consecutive-sequence/uraflower.js
Copy link
Contributor

Choose a reason for hiding this comment

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

그리고 저는 정렬 후 요소 체크할 때 동일한 숫자가 있는 경우 건너뛰는 처리 방식으로 구현했는데, Set을 사용해 구현하신 걸 보고 Set을 활용할 수도 있었겠구나, 라는 생각을 했습니다!
1주차 고생하셨습니다~!

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 주어진 배열의 숫자들로 만들 수 있는 가장 긴 연속 수열의 길이를 반환하는 함수
* @param {number[]} nums
* @return {number}
*/
const longestConsecutive = function(nums) {
const sorted = Array.from(new Set(nums)).sort((a, b) => Number(a) - Number(b));

let maxLength = 0;
let currentSequenceLength = 0;

for (let i = 0; i < sorted.length; i++) {
if (i === 0) {
maxLength = 1;
currentSequenceLength = 1;
continue;
}

if (sorted[i] === sorted[i - 1] + 1) {
currentSequenceLength += 1;
} else {
currentSequenceLength = 1;
}

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

return maxLength;
};

// 시간복잡도: O(n)
// 공간복잡도: O(n)
16 changes: 16 additions & 0 deletions top-k-frequent-elements/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 주어진 배열에서 가장 많이 속해있는 숫자 k개를 반환하는 함수
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
const topKFrequent = function(nums, k) {
const count = {};
nums.forEach((num) => {
count[num] = count[num] + 1 || 1;
});
return Object.keys(count).sort((a, b) => count[b] - count[a]).slice(0, k).map(Number);
};

// 시간복잡도: O(n)
// 공간복잡도: O(n)
19 changes: 19 additions & 0 deletions two-sum/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 주어진 배열 중 두 숫자의 합이 타겟일 때, 두 숫자의 인덱스를 반환하는 함수
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const twoSum = function (nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const diff = target - nums[i];
if (map.has(diff)) {
return [map.get(diff), i];
}
map.set(nums[i], i);
}
};

// 시간복잡도: O(n)
// 공간복잡도: O(n)