Skip to content

Commit 826eb07

Browse files
authored
Merge pull request #1430 from byol-han/main
[byol-han] WEEK 06 solutions
2 parents 6d09629 + e08cf3a commit 826eb07

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* https://leetcode.com/problems/container-with-most-water/
3+
* @param {number[]} height
4+
* @return {number}
5+
*/
6+
var maxArea = function (height) {
7+
let left = 0;
8+
let right = height.length - 1;
9+
let maxWater = 0;
10+
11+
while (left < right) {
12+
let width = right - left; // ํ˜„์žฌ ๋„ˆ๋น„๋Š” ๋‘ ํฌ์ธํ„ฐ ์‚ฌ์ด ๊ฑฐ๋ฆฌ
13+
let minHeight = Math.min(height[left], height[right]); // ํ˜„์žฌ ๋†’์ด๋Š” ๋‘ ์„  ์ค‘ ๋” ๋‚ฎ์€ ๊ฐ’ (๋ฌผ์ด ๋„˜์น˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๋‚ฎ์€ ์„  ๊ธฐ์ค€)
14+
let area = width * minHeight; // ํ˜„์žฌ ๊ตฌ๊ฐ„์ด ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ์˜ ์–‘ ๊ณ„์‚ฐ
15+
16+
maxWater = Math.max(maxWater, area); // ์ตœ๋Œ€ ๋ฌผ ์ €์žฅ๋Ÿ‰ ๊ฐฑ์‹ 
17+
18+
// ๋‚ฎ์€ ์ชฝ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™์‹œ์ผœ์„œ ๋” ํฐ ๋†’์ด๋ฅผ ์ฐพ์Œ
19+
if (height[left] < height[right]) {
20+
left++;
21+
} else {
22+
right--;
23+
}
24+
}
25+
26+
return maxWater;
27+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// ๊ฐ ๋ฌธ์ž ๋…ธ๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” TrieNode ํด๋ž˜์Šค
2+
class TrieNode {
3+
constructor() {
4+
this.children = {}; // ์ž์‹ ๋…ธ๋“œ๋“ค์„ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด (์˜ˆ: { a: TrieNode, b: TrieNode, ... })
5+
this.isEndOfWord = false; // ๋‹จ์–ด๊ฐ€ ์ด ๋…ธ๋“œ์—์„œ ๋๋‚˜๋Š”์ง€ ์—ฌ๋ถ€
6+
}
7+
}
8+
9+
class WordDictionary {
10+
constructor() {
11+
this.root = new TrieNode(); // ๋ฃจํŠธ ๋…ธ๋“œ ์ƒ์„ฑ (๋ชจ๋“  ๋‹จ์–ด์˜ ์‹œ์ž‘์ )
12+
}
13+
14+
/**
15+
* ๋‹จ์–ด๋ฅผ Trie์— ์ถ”๊ฐ€
16+
* @param {string} word
17+
*/
18+
addWord(word) {
19+
let node = this.root;
20+
for (let char of word) {
21+
// ํ˜„์žฌ ๋ฌธ์ž๊ฐ€ ์ž์‹ ๋…ธ๋“œ์— ์—†์œผ๋ฉด ์ƒˆ ๋…ธ๋“œ ์ƒ์„ฑ
22+
if (!node.children[char]) {
23+
node.children[char] = new TrieNode();
24+
}
25+
// ๋‹ค์Œ ๋ฌธ์ž๋กœ ์ด๋™
26+
node = node.children[char];
27+
}
28+
// ๋‹จ์–ด๊ฐ€ ๋๋‚˜๋Š” ์ง€์  ํ‘œ์‹œ
29+
node.isEndOfWord = true;
30+
}
31+
32+
/**
33+
* ๋‹จ์–ด ๊ฒ€์ƒ‰ (.์€ ์™€์ผ๋“œ์นด๋“œ๋กœ ์–ด๋–ค ๋ฌธ์ž๋“  ๋Œ€์ฒด ๊ฐ€๋Šฅ)
34+
* @param {string} word
35+
* @returns {boolean}
36+
*/
37+
search(word) {
38+
/**
39+
* DFS ๋ฐฉ์‹์œผ๋กœ ๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ (๋ฐฑํŠธ๋ž˜ํ‚น ํฌํ•จ)
40+
* @param {TrieNode} node - ํ˜„์žฌ ๋…ธ๋“œ
41+
* @param {number} i - ํ˜„์žฌ ํƒ์ƒ‰ ์ค‘์ธ ๋ฌธ์ž ์ธ๋ฑ์Šค
42+
*/
43+
const dfs = (node, i) => {
44+
// ๋‹จ์–ด ๋์— ๋„๋‹ฌํ•˜๋ฉด isEndOfWord ์—ฌ๋ถ€ ๋ฐ˜ํ™˜
45+
if (i === word.length) {
46+
return node.isEndOfWord;
47+
}
48+
49+
const char = word[i];
50+
51+
// ํ˜„์žฌ ๋ฌธ์ž๊ฐ€ '.'์ผ ๊ฒฝ์šฐ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ ํƒ์ƒ‰
52+
if (char === ".") {
53+
for (let child of Object.values(node.children)) {
54+
// ํ•˜๋‚˜๋ผ๋„ ์„ฑ๊ณตํ•˜๋ฉด true ๋ฐ˜ํ™˜
55+
if (dfs(child, i + 1)) return true;
56+
}
57+
// ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์‹คํŒจํ•˜๋ฉด false ๋ฐ˜ํ™˜
58+
return false;
59+
} else {
60+
// ํ•ด๋‹น ๋ฌธ์ž๋กœ ์—ฐ๊ฒฐ๋œ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด false
61+
if (!node.children[char]) return false;
62+
// ๋‹ค์Œ ๋ฌธ์ž๋กœ ์žฌ๊ท€ ํ˜ธ์ถœ
63+
return dfs(node.children[char], i + 1);
64+
}
65+
};
66+
67+
// ๋ฃจํŠธ ๋…ธ๋“œ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ ์‹œ์ž‘
68+
return dfs(this.root, 0);
69+
}
70+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* https://leetcode.com/problems/longest-increasing-subsequence/
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
var lengthOfLIS = function (nums) {
7+
if (nums.length === 0) return 0;
8+
9+
const dp = new Array(nums.length).fill(1); // ์ตœ์†Œ ๊ธธ์ด๋Š” 1 (์ž๊ธฐ ์ž์‹ ๋งŒ ํฌํ•จ)
10+
11+
for (let i = 1; i < nums.length; i++) {
12+
// ํ˜„์žฌ ์ˆซ์ž ์ด์ „์˜ ์ˆซ์ž๋“ค๊ณผ ๋น„๊ต
13+
for (let j = 0; j < i; j++) {
14+
// ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ์ธ์ง€ ํ™•์ธ
15+
if (nums[i] > nums[j]) {
16+
// ์ฆ๊ฐ€ํ•˜๋Š” subsequence๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋ฉด ๊ธธ์ด ๊ฐฑ์‹ 
17+
dp[i] = Math.max(dp[i], dp[j] + 1);
18+
}
19+
}
20+
}
21+
22+
return Math.max(...dp);
23+
};

โ€Žspiral-matrix/byol-han.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* https://leetcode.com/problems/spiral-matrix/
3+
* @param {number[][]} matrix
4+
* @return {number[]}
5+
*/
6+
var spiralOrder = function (matrix) {
7+
const result = [];
8+
if (matrix.length === 0) return result;
9+
10+
let top = 0;
11+
let bottom = matrix.length - 1;
12+
let left = 0;
13+
let right = matrix[0].length - 1;
14+
15+
while (top <= bottom && left <= right) {
16+
// 1. ์ขŒ -> ์šฐ
17+
for (let col = left; col <= right; col++) {
18+
result.push(matrix[top][col]);
19+
}
20+
top++; // ์œ„์ชฝ ๊ฒฝ๊ณ„ ํ•œ ์ค„ ์•„๋ž˜๋กœ ์ด๋™
21+
22+
// 2. ์œ„ -> ์•„๋ž˜
23+
for (let row = top; row <= bottom; row++) {
24+
result.push(matrix[row][right]);
25+
}
26+
right--; // ์˜ค๋ฅธ์ชฝ ๊ฒฝ๊ณ„ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
27+
28+
// 3. ์šฐ -> ์ขŒ
29+
if (top <= bottom) {
30+
for (let col = right; col >= left; col--) {
31+
result.push(matrix[bottom][col]);
32+
}
33+
bottom--; // ์•„๋ž˜์ชฝ ๊ฒฝ๊ณ„ ์œ„๋กœ ์ด๋™
34+
}
35+
36+
// 4. ์•„๋ž˜ -> ์œ„
37+
if (left <= right) {
38+
for (let row = bottom; row >= top; row--) {
39+
result.push(matrix[row][left]);
40+
}
41+
left++; // ์™ผ์ชฝ ๊ฒฝ๊ณ„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
42+
}
43+
}
44+
45+
return result;
46+
};

โ€Žvalid-parentheses/byol-han.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function isValid(s) {
2+
const stack = [];
3+
const bracketMap = {
4+
")": "(",
5+
"}": "{",
6+
"]": "[",
7+
};
8+
9+
for (let char of s) {
10+
if (char === "(" || char === "{" || char === "[") {
11+
stack.push(char);
12+
} else {
13+
// ๋‹ซ๋Š” ๊ด„ํ˜ธ๊ฐ€ ๋‚˜์™”์„ ๋•Œ ์Šคํƒ์ด ๋น„์—ˆ๊ฑฐ๋‚˜ ์ง์ด ์•ˆ ๋งž๋Š” ๊ฒฝ์šฐ
14+
if (stack.pop() !== bracketMap[char]) {
15+
return false;
16+
}
17+
}
18+
}
19+
20+
// ๋ชจ๋“  ๊ด„ํ˜ธ๊ฐ€ ์ง์ง€์–ด์กŒ๋Š”์ง€ ํ™•์ธ
21+
return stack.length === 0;
22+
}

0 commit comments

Comments
ย (0)