Skip to content

Commit 87e5f67

Browse files
authored
Merge pull request #1424 from uraflower/main
[uarflower] WEEK 06 solutions
2 parents 817b7c2 + 9d0ba97 commit 87e5f67

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ (๋‘ ์›์†Œ ์ค‘ ์ž‘์€ ๊ฐ’) * (๋‘ ์›์†Œ์˜ ์ธ๋ฑ์Šค ์ฐจ์ด)์˜ ์ตœ๋Œ“๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number[]} height
4+
* @return {number}
5+
*/
6+
const maxArea = function(height) {
7+
let left = 0;
8+
let right = height.length - 1;
9+
let max = 0;
10+
11+
while (left < right) {
12+
const w = right - left;
13+
const h = Math.min(height[left], height[right]);
14+
15+
max = Math.max(max, w * h);
16+
17+
if (height[left] <= height[right]) {
18+
left++;
19+
} else {
20+
right--;
21+
}
22+
}
23+
24+
return max;
25+
};
26+
27+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
28+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class TrieNode {
2+
constructor(value) {
3+
this.value = value;
4+
this.children = {};
5+
this.end = false;
6+
}
7+
}
8+
9+
class WordDictionary {
10+
constructor() {
11+
this.root = new TrieNode(null);
12+
}
13+
14+
/**
15+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(w) (w: word.length)
16+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(w)
17+
* @param {string} word
18+
* @return {void}
19+
*/
20+
addWord(word) {
21+
let current = this.root;
22+
23+
for (const char of word) {
24+
if (!current.children[char]) {
25+
const child = new TrieNode(char);
26+
current.children[char] = child;
27+
}
28+
current = current.children[char];
29+
}
30+
31+
current.end = true;
32+
};
33+
34+
/**
35+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(k * w) (k: children์˜ ๊ธธ์ด๋กœ ์ตœ๋Œ€ 26, w: word.length)
36+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(w)
37+
* @param {string} word
38+
* @return {boolean}
39+
*/
40+
search(word) {
41+
return this.#search(this.root, word, 0);
42+
};
43+
44+
#search(node, word, idx) {
45+
if (!node) return false;
46+
if (idx >= word.length) return node.end;
47+
48+
if (word[idx] === '.') {
49+
for (const current of Object.values(node.children)) {
50+
if (this.#search(current, word, idx + 1)) {
51+
return true;
52+
}
53+
}
54+
return false;
55+
} else {
56+
return this.#search(node.children[word[idx]], word, idx + 1);
57+
}
58+
}
59+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
const lengthOfLIS = function (nums) {
7+
const dp = Array(nums.length).fill(1);
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
for (let j = 0; j < i; j++) {
11+
if (nums[j] < nums[i])
12+
dp[i] = Math.max(dp[j] + 1, dp[i]);
13+
}
14+
}
15+
16+
return Math.max(...dp);
17+
};
18+
19+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n^2)
20+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)

โ€Žspiral-matrix/uraflower.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ํ–‰๋ ฌ์„ ๋‚˜์„ ํ˜•(์šฐ-ํ•˜-์ขŒ-์ƒ)์œผ๋กœ ์ˆœํšŒํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number[][]} matrix
4+
* @return {number[]}
5+
*/
6+
const spiralOrder = function (matrix) {
7+
const rows = matrix.length;
8+
const cols = matrix[0].length;
9+
let r = 0;
10+
let c = 0;
11+
let dr = 0; // 0, 1, 0, -1
12+
let dc = 1; // 1, 0, -1, 0
13+
14+
const output = [];
15+
16+
for (let i = 0; i < rows * cols; i++) {
17+
output.push(matrix[r][c]);
18+
matrix[r][c] = null;
19+
20+
// ๋ฐฉํ–ฅ์„ ์ „ํ™˜ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
21+
if (!(0 <= r + dr && r + dr < rows && 0 <= c + dc && c + dc < cols) || matrix[r + dr][c + dc] === null) {
22+
[dr, dc] = [dc, -dr];
23+
}
24+
25+
r += dr;
26+
c += dc;
27+
}
28+
29+
return output;
30+
};
31+
32+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(r * c)
33+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

โ€Žvalid-parentheses/uraflower.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ๊ด„ํ˜ธ ์Œ์ด ์•Œ๋งž๋Š”์ง€ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {string} s
4+
* @return {boolean}
5+
*/
6+
const isValid = function (s) {
7+
const stack = [];
8+
const pairs = {
9+
'(': ')',
10+
'{': '}',
11+
'[': ']',
12+
}
13+
14+
for (const bracket of s) {
15+
if (bracket in pairs) {
16+
stack.push(bracket);
17+
continue;
18+
}
19+
20+
const popped = stack.pop();
21+
if (bracket !== pairs[popped]) {
22+
return false;
23+
}
24+
}
25+
26+
return stack.length === 0;
27+
};
28+
29+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
30+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)

0 commit comments

Comments
ย (0)