diff --git a/container-with-most-water/jdy8739.js b/container-with-most-water/jdy8739.js new file mode 100644 index 000000000..2e6935370 --- /dev/null +++ b/container-with-most-water/jdy8739.js @@ -0,0 +1,32 @@ +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function(height) { + let max = 0; + + let startIdx = 0; + let endIdx = height.length - 1; + + while (startIdx < endIdx) { + const start = height[startIdx]; + const end = height[endIdx]; + + const gap = endIdx - startIdx; + const min = Math.min(start, end); + + const area = gap * min; + + max = Math.max(max, area); + + if (start < end) startIdx++; + else endIdx--; + } + + return max; +}; + +// 시간복잡도 O(n) +// n은 주어진 배열(height)의 길이 + + diff --git a/spiral-matrix/jdy8739.js b/spiral-matrix/jdy8739.js new file mode 100644 index 000000000..136bb348c --- /dev/null +++ b/spiral-matrix/jdy8739.js @@ -0,0 +1,49 @@ +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function(matrix) { + let top = 0; + let left = 0; + let bottom = matrix.length - 1; + let right = matrix[0].length - 1; + + const answer = []; + + while (top <= bottom && left <= right) { + for (let i = left; i <= right; i++) { + answer.push(matrix[top][i]); + } + top++; + + if (top > bottom) { + break; + } + + for (let j = top; j <= bottom; j++) { + answer.push(matrix[j][right]); + } + right--; + + if (left > right) { + break; + } + + for (let k = right; k >= left; k--) { + answer.push(matrix[bottom][k]); + } + bottom--; + + for (let l = bottom; l >= top; l--) { + answer.push(matrix[l][left]); + } + left++; + } + + return answer; +}; + +// 시간 복잡도 O(m * n) +// 공간 복잡도 O(1) + + diff --git a/valid-parentheses/jdy8739.js b/valid-parentheses/jdy8739.js new file mode 100644 index 000000000..194fbc83f --- /dev/null +++ b/valid-parentheses/jdy8739.js @@ -0,0 +1,30 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function(s) { + const stack = []; + + for (let i=0; i