diff --git a/container-with-most-water/krokerdile.js b/container-with-most-water/krokerdile.js new file mode 100644 index 000000000..1917ed229 --- /dev/null +++ b/container-with-most-water/krokerdile.js @@ -0,0 +1,21 @@ +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function(height) { + let left = 0; + let right = height.length - 1; + let max = 0; + + while(right > left){ + if(height[left] >= height[right]){ + max = Math.max(max, height[right] * (right - left)); + right = right -1; + } + else{ + max = Math.max(max, height[left] * (right - left)); + left = left +1; + } + } + return max; +}; diff --git a/design-add-and-search-words-data-structure/krokerdile.js b/design-add-and-search-words-data-structure/krokerdile.js new file mode 100644 index 000000000..bede4a46c --- /dev/null +++ b/design-add-and-search-words-data-structure/krokerdile.js @@ -0,0 +1,53 @@ +class TrieNode { + constructor() { + this.children = {}; // 문자 -> TrieNode 매핑 + this.isEnd = false; // 단어의 끝 표시 + } + } + + class WordDictionary { + constructor() { + this.root = new TrieNode(); + } + + /** + * 단어를 트라이에 추가 + * Time Complexity: O(L) // L = word.length + * Space Complexity: O(L) // 새로운 노드 최대 L개 추가 가능 + */ + addWord(word) { + let node = this.root; + for (const char of word) { + if (!node.children[char]) { + node.children[char] = new TrieNode(); + } + node = node.children[char]; + } + node.isEnd = true; + } + + /** + * 단어 또는 패턴 검색 + * Time Complexity: O(26^D * L) // D = '.' 개수 (최대 2), L = word.length + * Space Complexity: O(L) // DFS 재귀 호출 스택 깊이 + */ + search(word) { + const dfs = (index, node) => { + for (let i = index; i < word.length; i++) { + const char = word[i]; + if (char === '.') { + for (const child of Object.values(node.children)) { + if (dfs(i + 1, child)) return true; + } + return false; + } else { + if (!node.children[char]) return false; + node = node.children[char]; + } + } + return node.isEnd; + }; + + return dfs(0, this.root); + } + } diff --git a/longest-increasing-subsequence/krokerdile.js b/longest-increasing-subsequence/krokerdile.js new file mode 100644 index 000000000..5c40ffdb5 --- /dev/null +++ b/longest-increasing-subsequence/krokerdile.js @@ -0,0 +1,23 @@ +// Time: O(n log n), Space: O(n) +function lengthOfLIS(nums) { + const tails = []; + + for (const num of nums) { + let left = 0, right = tails.length; + + // 이진 탐색: tails에서 num이 들어갈 최소 위치 찾기 + while (left < right) { + const mid = Math.floor((left + right) / 2); + if (tails[mid] < num) { + left = mid + 1; + } else { + right = mid; + } + } + + // left는 삽입 위치 + tails[left] = num; + } + + return tails.length; +} diff --git a/spiral-matrix/krokerdile.js b/spiral-matrix/krokerdile.js new file mode 100644 index 000000000..8e059613c --- /dev/null +++ b/spiral-matrix/krokerdile.js @@ -0,0 +1,42 @@ +// Time: O(m * n) +// Space: O(1) + output array +function spiralOrder(matrix) { + const result = []; + + let top = 0; + let bottom = matrix.length - 1; + let left = 0; + let right = matrix[0].length - 1; + + while (top <= bottom && left <= right) { + // 1. 왼 → 오 + for (let i = left; i <= right; i++) { + result.push(matrix[top][i]); + } + top++; + + // 2. 위 → 아래 + for (let i = top; i <= bottom; i++) { + result.push(matrix[i][right]); + } + right--; + + // 3. 오 → 왼 + if (top <= bottom) { + for (let i = right; i >= left; i--) { + result.push(matrix[bottom][i]); + } + bottom--; + } + + // 4. 아래 → 위 + if (left <= right) { + for (let i = bottom; i >= top; i--) { + result.push(matrix[i][left]); + } + left++; + } + } + + return result; +} diff --git a/valid-parentheses/krokerdile.js b/valid-parentheses/krokerdile.js new file mode 100644 index 000000000..6c7b69ec9 --- /dev/null +++ b/valid-parentheses/krokerdile.js @@ -0,0 +1,31 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function(s) { + let ch = {}; + ch[')'] = '('; + ch['}'] = '{'; + ch[']'] = '['; + + let list = s.split(''); + let stack = []; + + list.forEach((ele)=>{ + if(stack.length == 0){ + stack.push(ele); + }else{ + let len = stack.length; + if(stack[len-1] == ch[ele]){ + stack.pop(); + }else{ + stack.push(ele); + } + } + }) + if(stack.length == 0){ + return true; + }else{ + return false; + } +};