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; +}; 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; +}; 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; +}; 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; +};