From 5591312397699a5506feffe588429cedfe52ec35 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Thu, 19 Sep 2024 17:38:27 +0900 Subject: [PATCH 1/4] 1. Valid Parentheses --- valid-parentheses/sunjae95.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 valid-parentheses/sunjae95.js diff --git a/valid-parentheses/sunjae95.js b/valid-parentheses/sunjae95.js new file mode 100644 index 00000000..a3a4bbde --- /dev/null +++ b/valid-parentheses/sunjae95.js @@ -0,0 +1,32 @@ +/** + * @description + * brainstorming: + * stack + * + * n: length of s + * time complexity: O(n) + * space complexity: O(n) + */ +var isValid = function (s) { + const stack = []; + + for (let i = 0; i < s.length; i++) { + if (s[i] === "(" || s[i] === "{" || s[i] === "[") { + stack.push(s[i]); + continue; + } + + const top = stack.length ? stack[stack.length - 1] : null; + if (top === null) return false; + + if ( + (top === "(" && s[i] === ")") || + (top === "{" && s[i] === "}") || + (top === "[" && s[i] === "]") + ) { + stack.pop(); + } else return false; + } + + return !stack.length; +}; From d71b70a5f6828cd1407ef1f03775fcb8033947a2 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Fri, 20 Sep 2024 00:31:15 +0900 Subject: [PATCH 2/4] 2. Container With Most Water --- container-with-most-water/sunjae95.js | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 container-with-most-water/sunjae95.js diff --git a/container-with-most-water/sunjae95.js b/container-with-most-water/sunjae95.js new file mode 100644 index 00000000..236f54e1 --- /dev/null +++ b/container-with-most-water/sunjae95.js @@ -0,0 +1,28 @@ +/** + * @description + * brainstorming: + * 1. brute force -> time limited + * 2. two pointer + * + * n: length of height + * time complexity: O(n) + * space complexity: O(1) + */ +var maxArea = function (height) { + let answer = 0; + let start = 0; + let end = height.length - 1; + + while (start !== end) { + const w = end - start; + const h = Math.min(height[start], height[end]); + answer = Math.max(answer, w * h); + if (height[start] >= height[end]) { + end--; + } else if (height[start] < height[end]) { + start++; + } + } + + return answer; +}; From 06ac2faaf6032cbfc27f7276c90a3166ec76d96a Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Fri, 20 Sep 2024 19:54:13 +0900 Subject: [PATCH 3/4] 4. Longest Increasing Subsequence --- longest-increasing-subsequence/sunjae95.js | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 longest-increasing-subsequence/sunjae95.js diff --git a/longest-increasing-subsequence/sunjae95.js b/longest-increasing-subsequence/sunjae95.js new file mode 100644 index 00000000..ccf73082 --- /dev/null +++ b/longest-increasing-subsequence/sunjae95.js @@ -0,0 +1,35 @@ +/** + * @description + * brainstorming: + * 1. dfs -> time limited + * 2. memoization + dfs + * + * n: length of nums + * time complexity: O(n^2) + * space complexity: O(n) + */ +var lengthOfLIS = function (nums) { + const memo = new Array(nums.length).fill(-1); + let answer = 0; + + const dfs = (index) => { + if (memo[index] !== -1) return memo[index]; + + let maxLength = 1; + + for (let i = index + 1; i < nums.length; i++) { + if (nums[index] < nums[i]) { + maxLength = Math.max(maxLength, 1 + dfs(i)); + } + } + + memo[index] = maxLength; + return maxLength; + }; + + for (let i = 0; i < nums.length; i++) { + answer = Math.max(answer, dfs(i)); + } + + return answer; +}; From fffc8e1dd0b2ad12f116dacc022862ea327fa709 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Fri, 20 Sep 2024 22:29:07 +0900 Subject: [PATCH 4/4] 5. Spiral Matrix --- spiral-matrix/sunjae95.js | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 spiral-matrix/sunjae95.js diff --git a/spiral-matrix/sunjae95.js b/spiral-matrix/sunjae95.js new file mode 100644 index 00000000..4460d781 --- /dev/null +++ b/spiral-matrix/sunjae95.js @@ -0,0 +1,45 @@ +/** + * @description + * brainstorming: + * just implement question + * + * m: length of matrix + * n: length of matrix[i] + * time complexity: O(m * n) + * space complexity: O(m * n) + */ +var spiralOrder = function (matrix) { + let count = matrix.length * matrix[0].length; + let [r, c] = [1, 1]; + const answer = []; + const MAX_R = matrix.length + 2; + const MAX_C = matrix[0].length + 2; + const visited = Array.from({ length: MAX_R }, (_, i) => { + return Array.from( + { length: MAX_C }, + (_, j) => i === 0 || i === MAX_R - 1 || j === 0 || j === MAX_C - 1 + ); + }); + + while (count--) { + const check = { + left: c >= 1 && visited[r][c - 1], + right: c < MAX_C - 1 && visited[r][c + 1], + top: r >= 1 && visited[r - 1][c], + bottom: r < MAX_R - 1 && visited[r + 1][c], + }; + visited[r][c] = true; + answer.push(matrix[r - 1][c - 1]); + + if (check.left && check.top && check.bottom) c++; + else if (check.left && check.top && check.right) r++; + else if (check.right && check.top && check.bottom) c--; + else if (check.left && check.right && check.bottom) r--; + else if (check.left && check.top) c++; + else if (check.top && check.right) r++; + else if (check.right && check.bottom) c--; + else if (check.bottom && check.left) r--; + } + + return answer; +};