Skip to content

Commit c64a6d9

Browse files
authored
Merge pull request #480 from JEONGHWANMIN/main
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week6 ๋ฌธ์ œํ’€์ด
2 parents 94da0df + f819319 commit c64a6d9

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
3+
4+
/**
5+
* @param {number[]} height
6+
* @return {number}
7+
*/
8+
var maxArea = function(height) {
9+
let maxArea = 0;
10+
11+
let leftIdx = 0;
12+
let rightIdx = height.length - 1;
13+
14+
while (leftIdx <= rightIdx) {
15+
const minHeight = Math.min(height[leftIdx], height[rightIdx]);
16+
const distance = rightIdx - leftIdx
17+
18+
maxArea = Math.max(maxArea, distance * minHeight);
19+
20+
if (height[leftIdx] < height[rightIdx]) leftIdx++
21+
else rightIdx--
22+
}
23+
24+
return maxArea
25+
};
26+
27+
console.log(maxArea([1,8,6,2,5,4,8,3,7])) //49
28+
console.log(maxArea([1,1])) //1
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„
2+
// addWord: O(n) (n์€ ์ถ”๊ฐ€ํ•˜๋Š” ๋‹จ์–ด์˜ ๊ธธ์ด)
3+
// search: O(m^n) (m์€ ๊ฐ ๋…ธ๋“œ์˜ ์ตœ๋Œ€ ์ž์‹ ์ˆ˜, n์€ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋‹จ์–ด์˜ ๊ธธ์ด)
4+
5+
// ๊ณต๊ฐ„๋ณต์žก๋„
6+
// addWord: O(n) (n์€ ์ถ”๊ฐ€ํ•˜๋Š” ๋‹จ์–ด์˜ ๊ธธ์ด)
7+
// search: O(n) (n์€ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋‹จ์–ด์˜ ๊ธธ์ด, ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด)
8+
9+
class TrieNode {
10+
constructor() {
11+
this.children = {}
12+
this.endOfWord = false
13+
}
14+
}
15+
16+
class WordDictionary {
17+
constructor() {
18+
this.root = new TrieNode()
19+
}
20+
addWord(word) {
21+
let curNode = this.root
22+
23+
for (let i = 0 ; i < word.length; i++) {
24+
if (!curNode.children[word[i]]) {
25+
curNode.children[word[i]] = new TrieNode();
26+
}
27+
28+
curNode = curNode.children[word[i]];
29+
}
30+
31+
curNode.endOfWord = true
32+
}
33+
search(word) {
34+
return this.searchInNode(word, this.root);
35+
}
36+
37+
searchInNode(word, node) {
38+
let curNode = node;
39+
40+
for (let i = 0; i < word.length; i++) {
41+
if (word[i] === '.') {
42+
for (let key in curNode.children) {
43+
if (this.searchInNode(word.slice(i + 1), curNode.children[key])) return true
44+
}
45+
return false
46+
}
47+
48+
if (word[i] !== '.') {
49+
if (!curNode.children[word[i]]) return false
50+
curNode = curNode.children[word[i]];
51+
}
52+
}
53+
54+
return curNode.endOfWord
55+
}
56+
}
57+
58+
59+
const wordDictionary = new WordDictionary();
60+
wordDictionary.addWord("bad");
61+
wordDictionary.addWord("dad");
62+
wordDictionary.addWord("mad");
63+
console.log(wordDictionary.search("pad")); // return False
64+
console.log(wordDictionary.search("bad")); // return True
65+
console.log(wordDictionary.search(".ad")); // return True
66+
console.log(wordDictionary.search("b..")); // return True
67+
68+

โ€Žspiral-matrix/hwanmini.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n * m)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„ O(n * m)
3+
4+
const isVisited = (matrix, row, col) => {
5+
return matrix[row][col] === '#'
6+
}
7+
8+
/**
9+
* @param {number[][]} matrix
10+
* @return {number[]}
11+
*/
12+
var spiralOrder = function(matrix) {
13+
const result = []
14+
15+
const colLen = matrix[0].length;
16+
const rowLen = matrix.length
17+
18+
let direction = 'right'
19+
let row = 0;
20+
let col = 0;
21+
while (result.length < colLen * rowLen ) {
22+
result.push(matrix[row][col])
23+
matrix[row][col] = '#'
24+
25+
if (direction === 'right') {
26+
if (isVisited(matrix, row, col+1) || col === colLen - 1) {
27+
direction = 'down'
28+
row++
29+
} else {
30+
col++
31+
}
32+
} else if (direction === 'down') {
33+
if ((row + 1 < rowLen && isVisited(matrix, row+1, col) || row === rowLen - 1)) {
34+
direction = 'left'
35+
col--
36+
} else {
37+
row++
38+
}
39+
} else if (direction === 'left') {
40+
if ((col > 0 && isVisited(matrix, row, col-1) || col === 0)) {
41+
direction = 'up'
42+
row--
43+
} else {
44+
col--
45+
}
46+
} else if (direction === 'up') {
47+
if ( (row > 0 && isVisited(matrix,row-1, col))) {
48+
direction = 'right'
49+
col++
50+
} else {
51+
row--
52+
}
53+
}
54+
55+
}
56+
57+
return result
58+
};
59+
60+
61+
62+
console.log(spiralOrder([
63+
[1,2,3],
64+
[4,5,6],
65+
[7,8,9]])) // [1,2,3,6,9,8,7,4,5]
66+

โ€Žvalid-parentheses/hwanmini.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
3+
4+
/**
5+
* @param {string} s
6+
* @return {boolean}
7+
*/
8+
var isValid = function(s) {
9+
if (s.length % 2 !== 0) return false
10+
11+
const stack = []
12+
13+
const opener = {
14+
"(" : ")",
15+
"{": "}",
16+
"[": "]"
17+
}
18+
19+
for (let i = 0 ; i < s.length; i++) {
20+
if (s[i] in opener) {
21+
stack.push(s[i]);
22+
} else {
23+
if (opener[stack.at(-1)] === s[i]) {
24+
stack.pop()
25+
} else {
26+
return false
27+
}
28+
}
29+
}
30+
31+
32+
return stack.length === 0
33+
};
34+
35+
console.log(isValid("()")); // true
36+
console.log(isValid("()[]{}")); // true
37+
console.log(isValid("(]")); // false
38+
console.log(isValid("([])")); // true
39+
console.log(isValid("([}}])")); // true

0 commit comments

Comments
ย (0)