Skip to content

Commit 5090d33

Browse files
authored
Merge pull request #1868 from jun0811/main
[jun0811] WEEK 06 solutions
2 parents 0d3ac69 + d175f65 commit 5090d33

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var maxArea = function (height) {
6+
// 더 낮은 쪽을 안쪽으로 이동하는 방식으로
7+
8+
let start = 0;
9+
let end = height.length - 1;
10+
let res = -1;
11+
while (start <= end) {
12+
const v = Math.min(height[start], height[end]) * (end - start);
13+
if (v > res) {
14+
res = v;
15+
}
16+
17+
if (height[start] > height[end]) {
18+
end -= 1;
19+
} else {
20+
start += 1;
21+
}
22+
}
23+
return res;
24+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var WordDictionary = function () {
2+
this.trie = {}; // Trie 루트 노드
3+
};
4+
5+
/**
6+
* @param {string} word
7+
* @return {void}
8+
*/
9+
WordDictionary.prototype.addWord = function (word) {
10+
let current = this.trie;
11+
12+
// 각 문자마다 노드 생성
13+
for (let i = 0; i < word.length; i++) {
14+
const char = word[i];
15+
if (!current[char]) {
16+
current[char] = {};
17+
}
18+
current = current[char];
19+
}
20+
21+
// 단어의 끝 표시
22+
current.isEnd = true;
23+
};
24+
25+
/**
26+
* @param {string} word
27+
* @return {boolean}
28+
*/
29+
WordDictionary.prototype.search = function (word) {
30+
return dfs(word, 0, this.trie);
31+
};
32+
33+
/**
34+
* @param {string} word - 검색할 단어
35+
* @param {number} index - 현재 검사 중인 문자 인덱스
36+
* @param {object} node - 현재 Trie 노드
37+
* @return {boolean}
38+
*/
39+
function dfs(word, index, node) {
40+
// 단어 끝에 도달했으면 isEnd 확인
41+
if (index === word.length) {
42+
return !!node.isEnd;
43+
}
44+
45+
const char = word[index];
46+
47+
if (char === '.') {
48+
// '.'인 경우: 모든 자식 노드를 탐색
49+
for (let key in node) {
50+
if (key !== 'isEnd') {
51+
// isEnd 속성은 제외
52+
if (dfs(word, index + 1, node[key])) {
53+
return true;
54+
}
55+
}
56+
}
57+
return false;
58+
} else {
59+
// 일반 문자인 경우: 해당 문자의 노드로 이동
60+
if (!node[char]) {
61+
return false;
62+
}
63+
return dfs(word, index + 1, node[char]);
64+
}
65+
}

valid-parentheses/jun0811.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function (s) {
6+
const match = {
7+
')': '(',
8+
']': '[',
9+
'}': '{',
10+
};
11+
if (match[s[0]]) return false;
12+
const stack = [];
13+
for (const bracket of s) {
14+
if (bracket == '(' || bracket == '{' || bracket == '[') stack.push(bracket);
15+
else {
16+
const openBracket = stack.pop();
17+
if (match[bracket] != openBracket) return false;
18+
}
19+
}
20+
if (stack.length > 0) return false;
21+
return true;
22+
};

0 commit comments

Comments
 (0)