Skip to content

Commit 2942ddc

Browse files
authored
Merge pull request #2006 from smosco/main
[smosco] WEEK 01 solutions
2 parents 3855d18 + 383aaa4 commit 2942ddc

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
const containsDuplicate = (nums) => {
6+
let uniqueNumSet = new Set(nums);
7+
return uniqueNumSet.size !== nums.length;
8+
};

โ€Žhouse-robber/smosco.jsโ€Ž

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* House Robber ๋ฌธ์ œ
3+
*
4+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
5+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
6+
*
7+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ•:
8+
* - Dynamic Programming์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์œ„์น˜์—์„œ์˜ ์ตœ๋Œ€ ๊ธˆ์•ก์„ ๊ณ„์‚ฐ
9+
* - ํ˜„์žฌ ์ง‘์„ ํ„ธ ๊ฒฝ์šฐ: ์ „์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€๊ฐ’ + ํ˜„์žฌ ์ง‘์˜ ๊ธˆ์•ก
10+
* - ํ˜„์žฌ ์ง‘์„ ์•ˆ ํ„ธ ๊ฒฝ์šฐ: ์ด์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€๊ฐ’
11+
* - ๋‘ ๊ฒฝ์šฐ ์ค‘ ์ตœ๋Œ€๊ฐ’์„ ์„ ํƒ
12+
*/
13+
const rob = (nums) => {
14+
if (nums.length === 1) return nums[0];
15+
16+
let prev2 = nums[0]; // ์ „์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ธˆ์•ก
17+
let prev1 = Math.max(nums[0], nums[1]); // ์ด์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ธˆ์•ก
18+
19+
for (let i = 2; i < nums.length; i++) {
20+
const current = Math.max(prev1, prev2 + nums[i]);
21+
prev2 = prev1;
22+
prev1 = current;
23+
}
24+
25+
return prev1;
26+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const longestConsecutive = (nums) => {
6+
if (nums.length === 0) return 0;
7+
8+
const numSet = new Set(nums);
9+
let maxLength = 0;
10+
11+
for (const num of numSet) {
12+
// ์‹œํ€€์Šค ์‹œ์ž‘์ ๋งŒ ์ฒดํฌ
13+
if (!numSet.has(num - 1)) {
14+
let currentNum = num;
15+
let currentLength = 1;
16+
17+
// ์—ฐ์†๋œ ์ˆซ์ž ์นด์šดํŠธ
18+
while (numSet.has(currentNum + 1)) {
19+
currentNum++;
20+
currentLength++;
21+
}
22+
23+
maxLength = Math.max(maxLength, currentLength);
24+
}
25+
}
26+
27+
return maxLength;
28+
};

0 commit comments

Comments
ย (0)