Skip to content

Commit b504ef4

Browse files
authored
Merge pull request DaleStudy#640 from Jeehay28/main
[Jeehay Park(๋ฐ•์ง€ํ˜œ)] Week 1
2 parents 6ccc41c + f5ef578 commit b504ef4

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

โ€Žcontains-duplicate/Jeehay28.js

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

โ€Žhouse-robber/Jeehay28.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
6+
// Bottom-Up DP (with constant space) ๋ฐฉ์‹
7+
8+
// ์‹œ๊ฐ„ ๋ณต์žก๋„ (Time Complexity)
9+
// O(n) โ€” ์ž…๋ ฅ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ์— ๋น„๋ก€ํ•˜์—ฌ ์ˆ˜ํ–‰ ์‹œ๊ฐ„์ด ์ฆ๊ฐ€
10+
11+
// ๊ณต๊ฐ„ ๋ณต์žก๋„ (Space Complexity)
12+
// O(1) โ€” ์ž…๋ ฅ ํฌ๊ธฐ์— ๊ด€๊ณ„์—†์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด ์ผ์ •
13+
14+
15+
var rob = function (nums) {
16+
17+
if (nums.length === 1) {
18+
return nums[0];
19+
}
20+
21+
let robbed_2 = nums[0];
22+
let robbed_1 = Math.max(nums[0], nums[1]);
23+
24+
for (let i = 2; i < nums.length; i++) {
25+
26+
const temp = robbed_1;
27+
28+
robbed_1 = Math.max(robbed_1, robbed_2 + nums[i]);
29+
30+
robbed_2 = temp;
31+
32+
}
33+
34+
return robbed_1;
35+
36+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function (nums) {
6+
let result = [];
7+
let longest = [];
8+
9+
numsSorted = nums.sort((a, b) => a - b);
10+
11+
numsFiltered = [...new Set(numsSorted)];
12+
13+
for (let i = 0; i < numsFiltered.length; i++) {
14+
if (result.length === 0) {
15+
result.push(numsFiltered[i]);
16+
} else if (result.length >= 1) {
17+
if (numsFiltered[i] === result[result.length - 1] + 1) {
18+
result.push(numsFiltered[i]);
19+
} else {
20+
longest.push(result.length);
21+
result = [numsFiltered[i]];
22+
}
23+
}
24+
}
25+
26+
if (longest.length === 0) {
27+
return result.length;
28+
} else {
29+
return Math.max(Math.max(...longest), result.length);
30+
}
31+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
var topKFrequent = function (nums, k) {
7+
let result = [];
8+
9+
const obj = nums.reduce((acc, cur) => {
10+
acc[cur] = (acc[cur] || 0) + 1;
11+
return acc;
12+
}, {});
13+
14+
const frequentValues = Object.values(obj)
15+
.sort((a, b) => b - a)
16+
.slice(0, k);
17+
18+
for (const [key, value] of Object.entries(obj)) {
19+
if (frequentValues.includes(value)) {
20+
result.push(parseInt(key));
21+
}
22+
}
23+
24+
return result;
25+
};

โ€Žvalid-palindrome/Jeehay28.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function (s) {
6+
const reg = /[a-z0-9]/g;
7+
const converted = s.toLowerCase().match(reg);
8+
9+
if (converted === null) {
10+
return true;
11+
} else {
12+
const forward = converted.join("");
13+
const backward = converted.reverse().join("");
14+
15+
return forward === backward;
16+
}
17+
};

0 commit comments

Comments
ย (0)