diff --git a/container-with-most-water/hwanmini.js b/container-with-most-water/hwanmini.js new file mode 100644 index 000000000..1433d255b --- /dev/null +++ b/container-with-most-water/hwanmini.js @@ -0,0 +1,28 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function(height) { + let maxArea = 0; + + let leftIdx = 0; + let rightIdx = height.length - 1; + + while (leftIdx <= rightIdx) { + const minHeight = Math.min(height[leftIdx], height[rightIdx]); + const distance = rightIdx - leftIdx + + maxArea = Math.max(maxArea, distance * minHeight); + + if (height[leftIdx] < height[rightIdx]) leftIdx++ + else rightIdx-- + } + + return maxArea +}; + +console.log(maxArea([1,8,6,2,5,4,8,3,7])) //49 +console.log(maxArea([1,1])) //1 diff --git a/design-add-and-search-words-data-structure/hwanmini.js b/design-add-and-search-words-data-structure/hwanmini.js new file mode 100644 index 000000000..978e038ec --- /dev/null +++ b/design-add-and-search-words-data-structure/hwanmini.js @@ -0,0 +1,68 @@ +// 시간복잡도 +// addWord: O(n) (n은 추가하는 단어의 길이) +// search: O(m^n) (m은 각 노드의 최대 자식 수, n은 검색하는 단어의 길이) + +// 공간복잡도 +// addWord: O(n) (n은 추가하는 단어의 길이) +// search: O(n) (n은 검색하는 단어의 길이, 재귀 호출 스택의 깊이) + +class TrieNode { + constructor() { + this.children = {} + this.endOfWord = false + } +} + +class WordDictionary { + constructor() { + this.root = new TrieNode() + } + addWord(word) { + let curNode = this.root + + for (let i = 0 ; i < word.length; i++) { + if (!curNode.children[word[i]]) { + curNode.children[word[i]] = new TrieNode(); + } + + curNode = curNode.children[word[i]]; + } + + curNode.endOfWord = true + } + search(word) { + return this.searchInNode(word, this.root); + } + + searchInNode(word, node) { + let curNode = node; + + for (let i = 0; i < word.length; i++) { + if (word[i] === '.') { + for (let key in curNode.children) { + if (this.searchInNode(word.slice(i + 1), curNode.children[key])) return true + } + return false + } + + if (word[i] !== '.') { + if (!curNode.children[word[i]]) return false + curNode = curNode.children[word[i]]; + } + } + + return curNode.endOfWord + } +} + + +const wordDictionary = new WordDictionary(); +wordDictionary.addWord("bad"); +wordDictionary.addWord("dad"); +wordDictionary.addWord("mad"); +console.log(wordDictionary.search("pad")); // return False +console.log(wordDictionary.search("bad")); // return True +console.log(wordDictionary.search(".ad")); // return True +console.log(wordDictionary.search("b..")); // return True + + diff --git a/spiral-matrix/hwanmini.js b/spiral-matrix/hwanmini.js new file mode 100644 index 000000000..64d951288 --- /dev/null +++ b/spiral-matrix/hwanmini.js @@ -0,0 +1,66 @@ +// 시간복잡도 O(n * m) +// 공간복잡도 O(n * m) + +const isVisited = (matrix, row, col) => { + return matrix[row][col] === '#' +} + +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function(matrix) { + const result = [] + + const colLen = matrix[0].length; + const rowLen = matrix.length + + let direction = 'right' + let row = 0; + let col = 0; + while (result.length < colLen * rowLen ) { + result.push(matrix[row][col]) + matrix[row][col] = '#' + + if (direction === 'right') { + if (isVisited(matrix, row, col+1) || col === colLen - 1) { + direction = 'down' + row++ + } else { + col++ + } + } else if (direction === 'down') { + if ((row + 1 < rowLen && isVisited(matrix, row+1, col) || row === rowLen - 1)) { + direction = 'left' + col-- + } else { + row++ + } + } else if (direction === 'left') { + if ((col > 0 && isVisited(matrix, row, col-1) || col === 0)) { + direction = 'up' + row-- + } else { + col-- + } + } else if (direction === 'up') { + if ( (row > 0 && isVisited(matrix,row-1, col))) { + direction = 'right' + col++ + } else { + row-- + } + } + + } + + return result +}; + + + +console.log(spiralOrder([ + [1,2,3], + [4,5,6], + [7,8,9]])) // [1,2,3,6,9,8,7,4,5] + diff --git a/valid-parentheses/hwanmini.js b/valid-parentheses/hwanmini.js new file mode 100644 index 000000000..9fc1a06d0 --- /dev/null +++ b/valid-parentheses/hwanmini.js @@ -0,0 +1,39 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function(s) { + if (s.length % 2 !== 0) return false + + const stack = [] + + const opener = { + "(" : ")", + "{": "}", + "[": "]" + } + + for (let i = 0 ; i < s.length; i++) { + if (s[i] in opener) { + stack.push(s[i]); + } else { + if (opener[stack.at(-1)] === s[i]) { + stack.pop() + } else { + return false + } + } + } + + + return stack.length === 0 +}; + +console.log(isValid("()")); // true +console.log(isValid("()[]{}")); // true +console.log(isValid("(]")); // false +console.log(isValid("([])")); // true +console.log(isValid("([}}])")); // true