diff --git a/container-with-most-water/lhc0506.js b/container-with-most-water/lhc0506.js new file mode 100644 index 000000000..f8fd0f3f1 --- /dev/null +++ b/container-with-most-water/lhc0506.js @@ -0,0 +1,32 @@ +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function(height) { + let result = 0; + let left = 0; + let right = height.length - 1; + + while(left < right) { + const leftHeight = height[left]; + const rightHeight = height[right]; + const width = right - left; + + const minHeight = leftHeight < rightHeight ? leftHeight : rightHeight; + const area = minHeight * width; + + if (area > result) { + result = area; + } + + if (leftHeight <= rightHeight) { + left++; + } else { + right--; + } + } + return result; +}; + +// 시간 복잡도: O(n) +// 공간 복잡도: O(1) diff --git a/design-add-and-search-words-data-structure/lhc0506.js b/design-add-and-search-words-data-structure/lhc0506.js new file mode 100644 index 000000000..dfd61f78f --- /dev/null +++ b/design-add-and-search-words-data-structure/lhc0506.js @@ -0,0 +1,67 @@ +const Node = function() { + this.children = {}; + this.isEnd = false; +} + +var WordDictionary = function() { + this.root = new Node(); +}; + +/** + * @param {string} word + * @return {void} + */ +WordDictionary.prototype.addWord = function(word) { + let currentNode = this.root; + + for (let char of word) { + if (!currentNode.children[char]) { + currentNode.children[char] = new Node(); + } + currentNode = currentNode.children[char]; + } + + currentNode.isEnd = true; +}; + +/** + * @param {string} word + * @return {boolean} + */ +WordDictionary.prototype.search = function(word) { + if (word === undefined) return false; + return this._search(this.root, word, 0); +}; + +WordDictionary.prototype._search = function(node, word, index) { + if (index === word.length) { + return node.isEnd; + } + + const char = word[index]; + + if (char !== '.') { + const child = node.children[char]; + return child ? this._search(child, word, index + 1) : false; + } + + for (const key in node.children) { + if (this._search(node.children[key], word, index + 1)) { + return true; + } + } + + return false; +}; + +/** + * Your WordDictionary object will be instantiated and called as such: + * var obj = new WordDictionary() + * obj.addWord(word) + * var param_2 = obj.search(word) + */ + +// n: 단어수, m: 단어 길이 +// addWord: 시간 복잡도 O(m) +// search: 시간 복잡도 O(m) +// 공간 복잡도 O(n * m) diff --git a/longest-increasing-subsequence/lhc0506.js b/longest-increasing-subsequence/lhc0506.js new file mode 100644 index 000000000..5774216f4 --- /dev/null +++ b/longest-increasing-subsequence/lhc0506.js @@ -0,0 +1,20 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var lengthOfLIS = function(nums) { + const dp = new Array(nums.length).fill(1); + + for (let i = 0; i < nums.length; i++) { + for (let j = 0; j < i; j++) { + if (nums[j] < nums[i] && dp[j] >= dp[i]) { + dp[i] = dp[j] + 1; + } + } + } + + return Math.max(...dp); +}; + +// 시간 복잡도 : O(n^2) +// 공간 복잡도 : O(n) diff --git a/spiral-matrix/lhc0506.js b/spiral-matrix/lhc0506.js new file mode 100644 index 000000000..0aea65ae1 --- /dev/null +++ b/spiral-matrix/lhc0506.js @@ -0,0 +1,41 @@ +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function(matrix) { + if (matrix.length === 0) return []; + + const result = []; + const rows = matrix.length; + const cols = matrix[0].length; + + let top = 0, bottom = rows - 1, left = 0, right = cols - 1; + + while (top <= bottom && left <= right) { + for (let i = left; i <= right; i++) { + result.push(matrix[top][i]); + } + top += 1; + + for (let i = top; i <= bottom; i++) { + result.push(matrix[i][right]); + } + right -= 1; + + if (top <= bottom) { + for (let i = right; i >= left; i--) { + result.push(matrix[bottom][i]); + } + bottom -= 1; + } + + if (left <= right) { + for (let i = bottom; i >= top; i--) { + result.push(matrix[i][left]); + } + left += 1; + } + } + + return result; +}; diff --git a/valid-parentheses/lhc0506.js b/valid-parentheses/lhc0506.js new file mode 100644 index 000000000..812fd1341 --- /dev/null +++ b/valid-parentheses/lhc0506.js @@ -0,0 +1,39 @@ +/** + * @param {string} s + * @return {boolean} + */ + +const BRACKETS = { + SMALL_LEFT : '(', + SMALL_RIGHT : ')', + MIDIDUM_LEFT : '{', + MIDIDUM_RIGHT : '}', + LARGE_LEFT : '[', + LARGE_RIGHT : ']', +}; + +const LEFT_BRAKCETS_SET = new Set([BRACKETS.SMALL_LEFT, BRACKETS.MIDIDUM_LEFT, BRACKETS.LARGE_LEFT]); + +const BRACKET_MAPPER = { + [BRACKETS.SMALL_RIGHT]: BRACKETS.SMALL_LEFT, + [BRACKETS.MIDIDUM_RIGHT]: BRACKETS.MIDIDUM_LEFT, + [BRACKETS.LARGE_RIGHT]: BRACKETS.LARGE_LEFT, +}; + +var isValid = function(s) { + const stack = []; + + for (const bracket of s) { + if (LEFT_BRAKCETS_SET.has(bracket)) { + stack.push(bracket); + } else { + const poppedBracekt = stack.pop(); + + if (poppedBracekt !== BRACKET_MAPPER[bracket]) { + return false; + } + } + } + + return stack.length === 0; +};