Skip to content

Commit be68f4a

Browse files
reach0908KJH
andauthored
[reach0908] WEEK 01 solutions (#1685)
* solve: contains-duplicate * solution: two sum * solve: top-k-frequent-elements-1 * solve: longest-consecutive-sequence * solve: longest-consecutive-sequence2 ๊ฐœ์„  ๋ฌธ์ œ ํ’€์ด ์ถ”๊ฐ€ * solve: house-robber ๋ฌธ์ œ ํ’€์ด ์ถ”๊ฐ€ ๋ฐ DP ๋ฐฉ์‹์œผ๋กœ ๊ฐœ์„  --------- Co-authored-by: KJH <[email protected]>
1 parent 43737e5 commit be68f4a

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-0
lines changed

โ€Žcontains-duplicate/reach0908.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @description
3+
* time complexity: O(n)
4+
* space complexity: O(n)
5+
* ํ’€์ด ๋ฐฉ๋ฒ•:
6+
* - ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํ•ด์‰ฌํ…Œ์ด๋ธ”์„ ๊ตฌ์„ฑํ•˜๊ณ  ํ•ด์‰ฌ ํ…Œ์ด๋ธ”์— ์ด๋ฏธ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ early return ์„ ํ†ตํ•ด ์ค‘๋ณต๋จ์„ ๋ฐ˜ํ™˜
7+
* @param {number[]} nums
8+
* @return {boolean}
9+
*/
10+
const containsDuplicate = function (nums) {
11+
const hashTable = new Set();
12+
for (let i = 0; i < nums.length; i++) {
13+
if (hashTable.has(nums[i])) {
14+
return true;
15+
}
16+
hashTable.set(nums[i], i);
17+
}
18+
return false;
19+
};

โ€Žhouse-robber/reach0908.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @description
3+
* time complexity: O(2^n)
4+
* space complexity: O(n)
5+
* ํ’€์ด ์‹คํŒจ
6+
* ํ’€์ด ๋ฐฉ๋ฒ•: ์„ ํƒํ•œ ๊ฒฝ์šฐ์™€ ์„ ํƒํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•˜์—ฌ ์ตœ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
7+
* @param {number[]} nums
8+
* @return {number}
9+
*/
10+
const rob = function (nums) {
11+
console.log(nums);
12+
if (nums.length === 0) return 0;
13+
if (nums.length === 1) return nums[0];
14+
if (nums.length === 2) return Math.max(nums[0], nums[1]);
15+
16+
// ์„ ํƒํ•œ ๊ฒฝ์šฐ
17+
const selected = nums[0] + rob(nums.slice(2));
18+
// ์„ ํƒํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
19+
const unselected = rob(nums.slice(1));
20+
21+
const max = Math.max(selected, unselected);
22+
23+
return max;
24+
};
25+
26+
/**
27+
* @description
28+
* time complexity: O(n)
29+
* space complexity: O(n)
30+
* runtime: 100ms
31+
* ํ’€์ด ๋ฐฉ๋ฒ•: ์œ„ ํ’€์ด๊ฐ€ ํƒ€์ž„์•„์›ƒ์ด ๋‚จ, DP๋กœ ํ’€์–ด์•ผํ•จ์„ ์ธ์ง€ ํ›„ ํ’€์ด ๋ฐฉ๋ฒ• ๋ณ€๊ฒฝ
32+
* @param {number[]} nums
33+
* @return {number}
34+
*/
35+
const robSolution2 = function (nums) {
36+
const dp = new Array(nums.length).fill(0);
37+
38+
dp[0] = nums[0];
39+
dp[1] = Math.max(nums[0], nums[1]);
40+
41+
for (let i = 2; i < nums.length; i += 1) {
42+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
43+
}
44+
45+
return dp[nums.length - 1];
46+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @description
3+
* time complexity: O(n log n)
4+
* space complexity: O(n)
5+
* runtime: 57ms
6+
* ํ’€์ด ๋ฐฉ๋ฒ•: ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ  ์ •๋ ฌํ•œ ๋‹ค์Œ์— ์—ฐ์†๋œ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜์—ฌ ์ตœ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
7+
* @param {number[]} nums
8+
* @return {number}
9+
*/
10+
const longestConsecutive = function (nums) {
11+
if (nums.length === 0) return 0;
12+
const sortedNums = [...new Set(nums)].sort((a, b) => a - b);
13+
14+
let maxConsecutiveCount = 1;
15+
let currentConsecutiveCount = 1;
16+
17+
for (let i = 1; i < sortedNums.length; i += 1) {
18+
if (sortedNums[i] === sortedNums[i - 1] + 1) {
19+
currentConsecutiveCount += 1;
20+
} else {
21+
currentConsecutiveCount = 1;
22+
}
23+
24+
maxConsecutiveCount = Math.max(
25+
maxConsecutiveCount,
26+
currentConsecutiveCount
27+
);
28+
}
29+
30+
return maxConsecutiveCount;
31+
};
32+
33+
/**
34+
* @description
35+
* time complexity: O(n)
36+
* space complexity: O(n)
37+
* runtime: 36ms
38+
* ํ’€์ด ๋ฐฉ๋ฒ•: ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ  Set์„ ์‚ฌ์šฉํ•˜์—ฌ O(1) ์กฐํšŒ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•œ ๋‹ค์Œ์— ์—ฐ์†๋œ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜์—ฌ ์ตœ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
39+
* @param {number[]} nums
40+
* @return {number}
41+
*/
42+
const longestConsecutive2 = function (nums) {
43+
if (nums.length === 0) return 0;
44+
45+
// Set์„ ์‚ฌ์šฉํ•˜์—ฌ O(1) ์กฐํšŒ ๊ฐ€๋Šฅ
46+
const numSet = new Set(nums);
47+
let maxLength = 0;
48+
49+
for (const num of numSet) {
50+
// ํ˜„์žฌ ์ˆซ์ž๊ฐ€ ์—ฐ์† ์ˆ˜์—ด์˜ ์‹œ์ž‘์ ์ธ์ง€ ํ™•์ธ
51+
// num-1์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด num์ด ์‹œ์ž‘์ 
52+
if (!numSet.has(num - 1)) {
53+
let currentNum = num;
54+
let currentLength = 1;
55+
56+
// ์—ฐ์†๋œ ๋‹ค์Œ ์ˆซ์ž๋“ค์ด ์กด์žฌํ•˜๋Š” ๋™์•ˆ ๊ณ„์† ํƒ์ƒ‰
57+
while (numSet.has(currentNum + 1)) {
58+
currentNum += 1;
59+
currentLength += 1;
60+
}
61+
62+
// ์ตœ๋Œ€ ๊ธธ์ด ์—…๋ฐ์ดํŠธ
63+
maxLength = Math.max(maxLength, currentLength);
64+
}
65+
}
66+
67+
return maxLength;
68+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @description
3+
* time complexity: O(n log n)
4+
* space complexity: O(n)
5+
* runtime: 3ms
6+
* ํ’€์ด ๋ฐฉ๋ฒ•:
7+
* ํ•ด์‹œ๋งต์„ ํ†ตํ•ด ๊ฐ ์ˆซ์ž์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ณ„์‚ฐ
8+
* ํ•ด์‹œ๋งต์„ ์ •๋ ฌํ•˜์—ฌ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ
9+
* ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ k๋งŒํผ ์งค๋ผ์„œ ๋ฐ˜ํ™˜
10+
* @param {number[]} nums
11+
* @param {number} k
12+
* @return {number[]}
13+
*/
14+
const topKFrequent = function (nums, k) {
15+
const hashMap = new Map();
16+
17+
for (const num of nums) {
18+
if (hashMap.has(num)) {
19+
hashMap.set(num, hashMap.get(num) + 1);
20+
} else {
21+
hashMap.set(num, 1);
22+
}
23+
}
24+
25+
return Array.from(hashMap.entries())
26+
.sort((a, b) => b[1] - a[1])
27+
.slice(0, k)
28+
.map(([num]) => num);
29+
};

โ€Žtwo-sum/reach0908.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @description
3+
* time complexity: O(n^2)
4+
* space complexity: O(1)
5+
* ํ’€์ด ๋ฐฉ๋ฒ•:
6+
* ์นด์šดํ„ฐ๋ฅผ ํ†ตํ•ด ๋ฐ˜๋ณต๋ฌธ ๋Œ๊ธฐ
7+
* ๊ฒ‰์œผ๋กœ๋Š” ๋ฐ˜๋ณต๋ฌธ์ด ํ•œ๊ฐœ ๊ฐ™์•„๋ณด์ด์ง€๋งŒ ์ด์ค‘๋ฐ˜๋ณต๋ฌธ์ด๋‹ค.
8+
* @param {number[]} nums
9+
* @param {number} target
10+
* @return {number[]}
11+
*/
12+
const twoSumSoluton1 = function (nums, target) {
13+
let left = 0;
14+
let right = 1;
15+
while (left < nums.length - 1) {
16+
const sum = nums[left] + nums[right];
17+
if (sum === target) {
18+
return [left, right];
19+
}
20+
21+
if (right < nums.length - 1) {
22+
right += 1;
23+
} else {
24+
left += 1;
25+
right = left + 1;
26+
}
27+
}
28+
};
29+
30+
/**
31+
* @description
32+
* ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ๋ณด๊ณ  ๊ฐœ์„ ํ•œ ์†”๋ฃจ์…˜
33+
* time complexity: O(n)
34+
* space complexity: O(n)
35+
* ํ’€์ด ๋ฐฉ๋ฒ•:
36+
* ์ด์ „ ๊ฐ’๋“ค ์ค‘์— ์›ํ•˜๋Š” ๊ฐ’์ด ์žˆ๋Š”์ง€๋งŒ ํ™•์ธ ํ›„ ์ถ”์ถœ, ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ํฌ๊ฒŒ ๊ฐ์†Œ์‹œํ‚ด
37+
* ํ•˜์ง€๋งŒ ํ•ด์‰ฌ๋งต์„ ๋งŒ๋“ค์–ด์•ผํ•ด์„œ ๊ณต๊ฐ„๋ณต์žก๋„๋Š” O(n)์œผ๋กœ ๋ณ€๊ฒฝ
38+
* @param {number[]} nums
39+
* @param {number} target
40+
* @return {number[]}
41+
*/
42+
const twoSumSoluton2 = function (nums, target) {
43+
const hashMap = new Map();
44+
45+
for (let i = 0; i < nums.length; i += 1) {
46+
const calculatedTarget = target - nums[i];
47+
if (hashMap.has(calculatedTarget)) {
48+
return [i, hashMap.get(calculatedTarget)];
49+
}
50+
51+
hashMap.set(nums[i], i);
52+
}
53+
54+
return [];
55+
};

0 commit comments

Comments
ย (0)