Skip to content

Commit cc50603

Browse files
authored
Merge pull request #1423 from lhc0506/main
[lhc0506] WEEK 06 solutions
2 parents aa758aa + e2dccdc commit cc50603

File tree

5 files changed

+199
-0
lines changed

5 files changed

+199
-0
lines changed

container-with-most-water/lhc0506.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var maxArea = function(height) {
6+
let result = 0;
7+
let left = 0;
8+
let right = height.length - 1;
9+
10+
while(left < right) {
11+
const leftHeight = height[left];
12+
const rightHeight = height[right];
13+
const width = right - left;
14+
15+
const minHeight = leftHeight < rightHeight ? leftHeight : rightHeight;
16+
const area = minHeight * width;
17+
18+
if (area > result) {
19+
result = area;
20+
}
21+
22+
if (leftHeight <= rightHeight) {
23+
left++;
24+
} else {
25+
right--;
26+
}
27+
}
28+
return result;
29+
};
30+
31+
// 시간 복잡도: O(n)
32+
// 공간 복잡도: O(1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const Node = function() {
2+
this.children = {};
3+
this.isEnd = false;
4+
}
5+
6+
var WordDictionary = function() {
7+
this.root = new Node();
8+
};
9+
10+
/**
11+
* @param {string} word
12+
* @return {void}
13+
*/
14+
WordDictionary.prototype.addWord = function(word) {
15+
let currentNode = this.root;
16+
17+
for (let char of word) {
18+
if (!currentNode.children[char]) {
19+
currentNode.children[char] = new Node();
20+
}
21+
currentNode = currentNode.children[char];
22+
}
23+
24+
currentNode.isEnd = true;
25+
};
26+
27+
/**
28+
* @param {string} word
29+
* @return {boolean}
30+
*/
31+
WordDictionary.prototype.search = function(word) {
32+
if (word === undefined) return false;
33+
return this._search(this.root, word, 0);
34+
};
35+
36+
WordDictionary.prototype._search = function(node, word, index) {
37+
if (index === word.length) {
38+
return node.isEnd;
39+
}
40+
41+
const char = word[index];
42+
43+
if (char !== '.') {
44+
const child = node.children[char];
45+
return child ? this._search(child, word, index + 1) : false;
46+
}
47+
48+
for (const key in node.children) {
49+
if (this._search(node.children[key], word, index + 1)) {
50+
return true;
51+
}
52+
}
53+
54+
return false;
55+
};
56+
57+
/**
58+
* Your WordDictionary object will be instantiated and called as such:
59+
* var obj = new WordDictionary()
60+
* obj.addWord(word)
61+
* var param_2 = obj.search(word)
62+
*/
63+
64+
// n: 단어수, m: 단어 길이
65+
// addWord: 시간 복잡도 O(m)
66+
// search: 시간 복잡도 O(m)
67+
// 공간 복잡도 O(n * m)
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var lengthOfLIS = function(nums) {
6+
const dp = new Array(nums.length).fill(1);
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
for (let j = 0; j < i; j++) {
10+
if (nums[j] < nums[i] && dp[j] >= dp[i]) {
11+
dp[i] = dp[j] + 1;
12+
}
13+
}
14+
}
15+
16+
return Math.max(...dp);
17+
};
18+
19+
// 시간 복잡도 : O(n^2)
20+
// 공간 복잡도 : O(n)

spiral-matrix/lhc0506.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number[]}
4+
*/
5+
var spiralOrder = function(matrix) {
6+
if (matrix.length === 0) return [];
7+
8+
const result = [];
9+
const rows = matrix.length;
10+
const cols = matrix[0].length;
11+
12+
let top = 0, bottom = rows - 1, left = 0, right = cols - 1;
13+
14+
while (top <= bottom && left <= right) {
15+
for (let i = left; i <= right; i++) {
16+
result.push(matrix[top][i]);
17+
}
18+
top += 1;
19+
20+
for (let i = top; i <= bottom; i++) {
21+
result.push(matrix[i][right]);
22+
}
23+
right -= 1;
24+
25+
if (top <= bottom) {
26+
for (let i = right; i >= left; i--) {
27+
result.push(matrix[bottom][i]);
28+
}
29+
bottom -= 1;
30+
}
31+
32+
if (left <= right) {
33+
for (let i = bottom; i >= top; i--) {
34+
result.push(matrix[i][left]);
35+
}
36+
left += 1;
37+
}
38+
}
39+
40+
return result;
41+
};

valid-parentheses/lhc0506.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
6+
const BRACKETS = {
7+
SMALL_LEFT : '(',
8+
SMALL_RIGHT : ')',
9+
MIDIDUM_LEFT : '{',
10+
MIDIDUM_RIGHT : '}',
11+
LARGE_LEFT : '[',
12+
LARGE_RIGHT : ']',
13+
};
14+
15+
const LEFT_BRAKCETS_SET = new Set([BRACKETS.SMALL_LEFT, BRACKETS.MIDIDUM_LEFT, BRACKETS.LARGE_LEFT]);
16+
17+
const BRACKET_MAPPER = {
18+
[BRACKETS.SMALL_RIGHT]: BRACKETS.SMALL_LEFT,
19+
[BRACKETS.MIDIDUM_RIGHT]: BRACKETS.MIDIDUM_LEFT,
20+
[BRACKETS.LARGE_RIGHT]: BRACKETS.LARGE_LEFT,
21+
};
22+
23+
var isValid = function(s) {
24+
const stack = [];
25+
26+
for (const bracket of s) {
27+
if (LEFT_BRAKCETS_SET.has(bracket)) {
28+
stack.push(bracket);
29+
} else {
30+
const poppedBracekt = stack.pop();
31+
32+
if (poppedBracekt !== BRACKET_MAPPER[bracket]) {
33+
return false;
34+
}
35+
}
36+
}
37+
38+
return stack.length === 0;
39+
};

0 commit comments

Comments
 (0)