From b7b6ee84f17a191f1d99e06396a62865f3a3369d Mon Sep 17 00:00:00 2001 From: Finn <82873315+uraflower@users.noreply.github.com> Date: Mon, 5 May 2025 12:51:37 +0900 Subject: [PATCH 1/5] [ PS ] : Valid Parentheses --- valid-parentheses/uraflower.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 valid-parentheses/uraflower.js diff --git a/valid-parentheses/uraflower.js b/valid-parentheses/uraflower.js new file mode 100644 index 000000000..51e441f71 --- /dev/null +++ b/valid-parentheses/uraflower.js @@ -0,0 +1,30 @@ +/** + * 주어진 문자열의 괄호 쌍이 알맞는지 반환하는 함수 + * @param {string} s + * @return {boolean} + */ +const isValid = function (s) { + const stack = []; + const pairs = { + '(': ')', + '{': '}', + '[': ']', + } + + for (const bracket of s) { + if (bracket in pairs) { + stack.push(bracket); + continue; + } + + const popped = stack.pop(); + if (bracket !== pairs[popped]) { + return false; + } + } + + return stack.length === 0; +}; + +// 시간복잡도: O(n) +// 공간복잡도: O(n) From 52d8c2aba16d685a98de15ba67bc096b0f1e3697 Mon Sep 17 00:00:00 2001 From: Finn <82873315+uraflower@users.noreply.github.com> Date: Mon, 5 May 2025 18:09:55 +0900 Subject: [PATCH 2/5] [ PS ] : Container With Most Water --- container-with-most-water/uraflower.js | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 container-with-most-water/uraflower.js diff --git a/container-with-most-water/uraflower.js b/container-with-most-water/uraflower.js new file mode 100644 index 000000000..d4f1f43fd --- /dev/null +++ b/container-with-most-water/uraflower.js @@ -0,0 +1,28 @@ +/** + * 주어진 배열에서 (두 원소 중 작은 값) * (두 원소의 인덱스 차이)의 최댓값을 반환하는 함수 + * @param {number[]} height + * @return {number} + */ +const maxArea = function(height) { + let left = 0; + let right = height.length - 1; + let max = 0; + + while (left < right) { + const w = right - left; + const h = Math.min(height[left], height[right]); + + max = Math.max(max, w * h); + + if (height[left] <= height[right]) { + left++; + } else { + right--; + } + } + + return max; +}; + +// 시간복잡도: O(n) +// 공간복잡도: O(1) From f94110ef63bf4382f07537b4ee257919948b2423 Mon Sep 17 00:00:00 2001 From: Finn <82873315+uraflower@users.noreply.github.com> Date: Sun, 11 May 2025 17:07:44 +0900 Subject: [PATCH 3/5] [ PS ] : Design Add And Search Words Data Structure --- .../uraflower.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 design-add-and-search-words-data-structure/uraflower.js diff --git a/design-add-and-search-words-data-structure/uraflower.js b/design-add-and-search-words-data-structure/uraflower.js new file mode 100644 index 000000000..1a7ab139f --- /dev/null +++ b/design-add-and-search-words-data-structure/uraflower.js @@ -0,0 +1,59 @@ +class TrieNode { + constructor(value) { + this.value = value; + this.children = {}; + this.end = false; + } +} + +class WordDictionary { + constructor() { + this.root = new TrieNode(null); + } + + /** + * 시간복잡도: O(w) (w: word.length) + * 공간복잡도: O(w) + * @param {string} word + * @return {void} + */ + addWord(word) { + let current = this.root; + + for (const char of word) { + if (!current.children[char]) { + const child = new TrieNode(char); + current.children[char] = child; + } + current = current.children[char]; + } + + current.end = true; + }; + + /** + * 시간복잡도: O(k * w) (k: children의 길이로 최대 26, w: word.length) + * 공간복잡도: O(w) + * @param {string} word + * @return {boolean} + */ + search(word) { + return this.#search(this.root, word, 0); + }; + + #search(node, word, idx) { + if (!node) return false; + if (idx >= word.length) return node.end; + + if (word[idx] === '.') { + for (const current of Object.values(node.children)) { + if (this.#search(current, word, idx + 1)) { + return true; + } + } + return false; + } else { + return this.#search(node.children[word[idx]], word, idx + 1); + } + } +}; From adcc89118442171f2919a693e042a87493317287 Mon Sep 17 00:00:00 2001 From: Finn <82873315+uraflower@users.noreply.github.com> Date: Sun, 11 May 2025 17:55:28 +0900 Subject: [PATCH 4/5] [ PS ] : Longest Increasing Subsequence --- longest-increasing-subsequence/uraflower.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 longest-increasing-subsequence/uraflower.js diff --git a/longest-increasing-subsequence/uraflower.js b/longest-increasing-subsequence/uraflower.js new file mode 100644 index 000000000..356b1eb18 --- /dev/null +++ b/longest-increasing-subsequence/uraflower.js @@ -0,0 +1,20 @@ +/** + * 주어진 배열에서 가장 긴 증가하는 수열의 길이를 반환하는 함수 + * @param {number[]} nums + * @return {number} + */ +const lengthOfLIS = function (nums) { + const dp = 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[i] = Math.max(dp[j] + 1, dp[i]); + } + } + + return Math.max(...dp); +}; + +// 시간복잡도: O(n^2) +// 공간복잡도: O(n) From 9d0ba97304d8e49dcdf11e01a954426775783894 Mon Sep 17 00:00:00 2001 From: Finn <82873315+uraflower@users.noreply.github.com> Date: Sun, 11 May 2025 18:57:35 +0900 Subject: [PATCH 5/5] [ PS ] : Spiral Matrix --- spiral-matrix/uraflower.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 spiral-matrix/uraflower.js diff --git a/spiral-matrix/uraflower.js b/spiral-matrix/uraflower.js new file mode 100644 index 000000000..e256c69c4 --- /dev/null +++ b/spiral-matrix/uraflower.js @@ -0,0 +1,33 @@ +/** + * 주어진 행렬을 나선형(우-하-좌-상)으로 순회한 결과를 반환하는 함수 + * @param {number[][]} matrix + * @return {number[]} + */ +const spiralOrder = function (matrix) { + const rows = matrix.length; + const cols = matrix[0].length; + let r = 0; + let c = 0; + let dr = 0; // 0, 1, 0, -1 + let dc = 1; // 1, 0, -1, 0 + + const output = []; + + for (let i = 0; i < rows * cols; i++) { + output.push(matrix[r][c]); + matrix[r][c] = null; + + // 방향을 전환해야 하는 경우 + if (!(0 <= r + dr && r + dr < rows && 0 <= c + dc && c + dc < cols) || matrix[r + dr][c + dc] === null) { + [dr, dc] = [dc, -dr]; + } + + r += dr; + c += dc; + } + + return output; +}; + +// 시간복잡도: O(r * c) +// 공간복잡도: O(1)