Skip to content

[solbijae] WEEK 01 solutions #1680

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
Jul 27, 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/solbijae.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function containsDuplicate(nums: number[]): boolean {
const set = new Set(nums);
const setSize = set.size;
const numsLength = nums.length;

return setSize !== numsLength ? true : false;
Copy link
Contributor

Choose a reason for hiding this comment

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

return setSize === numsLength 만 사용해도 괜찮을것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오 그렇네요. 감사합니다!


// Big O
// 시간 복잡도: O(n) 한번씩 순회하면서 확인
// 공간 복잡도: O(n) Set을 사용해 배열 크기만큼 메모리가 더 사용됨
};
30 changes: 30 additions & 0 deletions house-robber/solbijae.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function rob(nums: number[]): number {
// 첫시도: 아예 떨어져있는 경우를 고려하지 않음 (통과 안됨)
// let evenSum = 0;
// let oddSum = 0;

// for (let i=0; i<nums.length; i++) {
// if (i % 2 === 0) {
// evenSum += nums[i];
// } else {
// oddSum += nums[i];
// }
// }

// return Math.max(evenSum, oddSum);

// 시간복잡도 O(n), 공간복잡도 O(1)
if (nums.length === 0) return 0;
if (nums.length === 1) return nums[0];

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

for (let i = 2; i < nums.length; i++) {
const current = Math.max(max, prev + nums[i]);
prev = max;
max = current;
}

return max;
};
44 changes: 44 additions & 0 deletions longest-consecutive-sequence/solbijae.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function longestConsecutive(nums: number[]): number {
// 첫 시도: sort때문에 시간 복잡도가 O(n log n)라 O(n)에 풀어야 한다는 요구사항 만족하지 못함, 공간복잡도는 O(n)
// if (nums.length === 0) return 0;
// const sorted = [...new Set(nums)].sort((a, b) => a-b);
// let max = 1;
// let current = 1;

// for (let i=0; i<nums.length; i++) {
// console.log(sorted[i])
// if (sorted[i+1] - sorted[i] === 1) {
// current += 1;
// } else {
// current = 1;
// }

// max = Math.max(max, current);
// }

// return max;

// 두번째 시도: 시간, 공간복잡도 O(n)
if (nums.length === 0) return 0;

const numSet = new Set(nums);
let current = 0;
let consecutive = 1;
let max = 1;

for (let num of numSet) {
if (!numSet.has(num - 1)) {
current = num;
consecutive = 1;

while (numSet.has(current + 1)) {
consecutive += 1;
current = current + 1;
}

max = Math.max(consecutive, max);
}
}

return max;
};
16 changes: 16 additions & 0 deletions top-k-frequent-elements/solbijae.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function topKFrequent(nums: number[], k: number): number[] {
const map = new Map();

for (let i=0; i<nums.length; i++) {
const num = nums[i]
map.set(num, (map.get(num)|| 0) + 1 );
}

return [...map.entries()]
.sort((a, b) => b[1] - a[1])
.slice(0, k)
.map(([key]) => key);
};

// 시간 복잡도: O(n log n)
// 공간 복잡도: O(n)
19 changes: 19 additions & 0 deletions two-sum/solbijae.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function twoSum(nums: number[], target: number): number[] {
// 첫 코드: 시간 복잡도가 O(n^2)로 개선 필요
// for (let i=0; i<nums.length-1; i++) {
// for (let j=1; j<nums.length; j++) {
// if (nums[i] + nums[j] === target) return [i, j];
// }
// }

// 시간 공간 모두 O(n)으로 만들기 위해 해시맵 사용
const map = new Map();
for (let i=0; i<nums.length; i++) {
const complement = target - nums[i];
if (map.get(complement) !== undefined) {
return [map.get(complement)!, i];
} else {
map.set(nums[i], i);
}
}
};