Skip to content

Commit 5ac3e9a

Browse files
authored
Merge pull request DaleStudy#682 from yeeZinu/main
[ํ˜ธ๋Œ์ด] Week 1
2 parents 2517417 + 243f35d commit 5ac3e9a

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

โ€Žcontains-duplicate/yeeZinu.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
5+
nums ๋ฐฐ์—ด๋‚ด์˜ ์•„๋ฌด ์ •์ˆ˜๋‚˜ 2๊ฐœ ์ด์ƒ ์ค‘๋ณต๋˜๋ฉด true๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ํ•จ์ˆ˜.
6+
7+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
8+
*/
9+
var containsDuplicate = function (nums) {
10+
return nums.length !== new Set(nums).size
11+
}
12+
13+
console.log(containsDuplicate([1, 2, 3, 1])); // true
14+
// console.log(containsDuplicate([1, 2, 3, 4])); // false
15+
// console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true

โ€Žhouse-robber/yeeZinu.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var rob = function (nums) {
2+
const n = nums.length;
3+
4+
// ๊ธธ์ด๊ฐ€ 1์ด๋ผ๋ฉด
5+
if (n === 1) {
6+
return nums[0];
7+
}
8+
9+
// nums์˜ ๊ธธ์ด๋งŒํผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ ๋ฐฐ์—ด
10+
const dp = Array(n).fill(0);
11+
12+
// 0๋ฒˆ์€ nums[0]
13+
dp[0] = nums[0];
14+
// 1๋ฒˆ์€ 0๊ณผ 1์ค‘ ํฐ๊ฒƒ์„ ์„ ํƒ
15+
dp[1] = Math.max(nums[0], nums[1]);
16+
17+
// i-1 ๊ณผ i + i-2 ์˜ ํ•ฉ์ค‘ ๋” ํฐ๊ฒƒ์„ ์„ ํƒํ•˜๋ฉด๋จ
18+
for (let i = 2; i < n; i++) {
19+
dp[i] = Math.max(dp[i - 1], nums[i] + dp[i - 2]);
20+
}
21+
// i๊ฐ€ n - 1๊นŒ์ง€ ๋ฐ˜๋ณตํ•จ
22+
return dp[n - 1];
23+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function (nums) {
6+
// set์œผ๋กœ ์ค‘๋ณต๋œ element ์ œ๊ฑฐ
7+
const numSet = new Set(nums);
8+
// ์ตœ๋Œ€ ์—ฐ์† ๊ธธ์ด ๋ณ€์ˆ˜
9+
let maxLength = 0;
10+
11+
// ์ตœ๋Œ€ ์—ฐ์† ๊ธธ์ด ์ฐพ๊ธฐ
12+
for (let num of numSet) {
13+
// ๋งŒ์•ฝ ํ˜„์žฌ ์ˆ˜ -1์ด ์—†๋‹ค๋ฉด?
14+
if (!numSet.has(num - 1)) {
15+
let currentNum = num; //ํ˜„์žฌ๊ฐ’์ด ์‹œ์ž‘
16+
let currentLength = 1; //์ตœ๋Œ€ ๊ธธ์ด 1๋กœ ์ดˆ๊ธฐํ™”
17+
18+
// ํ˜„์žฌ๊ฐ’ +1์ด ์žˆ์„ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณต
19+
while (numSet.has(currentNum + 1)) {
20+
currentNum++;
21+
currentLength++;
22+
}
23+
24+
// ์ตœ๋Œ€๊ธธ์ด ๊ฐ’๊ณผ ํ˜„์žฌ ๊ธธ์ด๊ฐ’์ค‘ ๋” ๋†’์€๊ฒƒ์ด ์ตœ๋Œ€๊ธธ์ด๊ฐ’
25+
maxLength = Math.max(maxLength, currentLength);
26+
}
27+
}
28+
return maxLength;
29+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*
6+
* nums ๋ฐฐ์—ด ๋‚ด ์ตœ๋นˆ๋„ ์ˆซ์ž k๊ฐœ ์ถœ๋ ฅ
7+
*
8+
*/
9+
var topKFrequent = function (nums, k) {
10+
// ๋นˆ๋„ ์ฒดํฌํ•  ๊ฐ์ฒด
11+
let frequent = {};
12+
13+
for (let i = 0; i < nums.length; i++) {
14+
// ์ˆซ์ž ์ค‘๋ณต๋ ๋•Œ๋งˆ๋‹ค +1
15+
if (frequent[nums[i]] > 0) {
16+
frequent[nums[i]]++;
17+
}
18+
// ์—†์œผ๋ฉด 1๋กœ ์ดˆ๊ธฐํ™”
19+
else {
20+
frequent[nums[i]] = 1;
21+
}
22+
}
23+
24+
// ์ •๋ ฌ์„ ์œ„ํ•ด entries๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
25+
const frequentEntries = Object.entries(frequent);
26+
frequentEntries.sort((a, b) => b[1] - a[1]); // ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
27+
28+
// k๊ฐฏ์ˆ˜๋งŒํผ ๋ฐฐ์—ด ์ž๋ฅด๊ธฐ ๋ฐ ๋ฐฐ์—ด ๋‚ด๋ถ€ ๊ฐ’ number๋กœ ๋ณ€๊ฒฝ
29+
const topK = frequentEntries.slice(0, k).map((i) => Number(i[0]));
30+
return topK;
31+
};

โ€Žtwo-sum/yeeZinu.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
7+
/*
8+
๋‘ ์ˆ˜ ๋”ํ•˜๊ธฐ
9+
nums: ์ˆซ์ž ๋ฐฐ์—ด
10+
target: ๋ชฉํ‘œ ์ˆซ์ž
11+
12+
target ์ˆซ์ž์— ๋งž๊ฒŒ nums ๋ฐฐ์—ด์ค‘ 2๊ฐœ๋ฅผ ๋”ํ•จ.
13+
return์€ ๋”ํ•œ ๋‘ ์ˆ˜์˜ index
14+
- for i in nums -> target - i ๊ฐ’์ด ๋ฐฐ์—ด๋‚ด์— ์žˆ๋Š”์ง€ ์ฐพ๊ธฐ
15+
*/
16+
var twoSum = function (nums, target) {
17+
let indexArray = []; // ๋‹ต์„ ์ €์žฅํ•  ๋ฐฐ์—ด
18+
for (let i = 0; i < nums.length; i++) {
19+
let tNum = nums.lastIndexOf(target - nums[i]); // ๋ฐฐ์—ด๋‚ด์— target - nums[i] ์ฐพ๊ธฐ
20+
if (i === tNum) { // ๊ฐ™์€ ์ˆ˜ ์‚ฌ์šฉ ๋ฐฉ์ง€
21+
22+
}
23+
else if (tNum > 0 && indexArray.lastIndexOf(i) === -1) { // ๋ฐฐ์—ด๋‚ด์— ์›ํ•˜๋Š” ๊ฐ’์ด ์žˆ๋‹ค๋ฉด
24+
indexArray.push(i);
25+
indexArray.push(tNum);
26+
break;
27+
}
28+
}
29+
return indexArray;
30+
};

โ€Žvalid-parentheses/yeeZinu.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
5+
๊ด„ํ˜ธ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ(
6+
์–ด๋А ๊ด„ํ˜ธ๋กœ ์—ด์–ด๋„ ์ƒ๊ด€์€ ์—†์ง€๋งŒ ์—ด์—ˆ์œผ๋ฉด ๋‹ซ์•„์•ผ์ง€ true ์•ˆ๋‹ซ์œผ๋ฉด false
7+
({)} -> ์ด๋Ÿฐ์‹์œผ๋กœ ์ค‘๊ฐ„์— ์„ž์—ฌ์„œ ๋‹ซํ˜€์žˆ์ง€ ์•Š๋‹ค๋Š” ์ „์ œ์˜ ๋ฌธ์ œ
8+
*/
9+
var isValid = function (s) {
10+
if (s.length % 2 === 1) {
11+
return false
12+
}
13+
14+
// ๊ด„ํ˜ธ๋“ค์ด ๋“ค์–ด๊ฐˆ ์Šคํƒ๋ฐฐ์—ด
15+
const stack = [];
16+
17+
// ๊ด„ํ˜ธ ์ง ๊ฐ์ฒด
18+
const pair = {
19+
"(": ")",
20+
"{": "}",
21+
"[": "]",
22+
}
23+
24+
for (let i = 0; i < s.length; i++) {
25+
// ์—ด๋ฆฐ๊ด„ํ˜ธ๋ฉด ์Šคํƒ์ถ”๊ฐ€
26+
if (s[i] in pair) {
27+
stack.push(s[i]);
28+
}
29+
// ๋‹ซํžŒ ๊ด„ํ˜ธ๋ผ๋ฉด?
30+
else {
31+
// ์Šคํƒ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰์ด ๊ฐ™์€ ์ข…๋ฅ˜์˜ ๊ด„ํ˜ธ๋ผ๋ฉด ์ œ๊ฑฐ
32+
if (pair[stack.at(-1)] === s[i]) {
33+
stack.pop();
34+
}
35+
// ์•„๋‹ˆ๋ฉด false
36+
else {
37+
return false
38+
}
39+
}
40+
}
41+
// ์Šคํƒ๋ฐฐ์—ด์ด ๋น„์—ˆ์œผ๋ฉด true
42+
return stack.length === 0;
43+
};

0 commit comments

Comments
ย (0)