diff --git a/container-with-most-water/robinyoon-dev.js b/container-with-most-water/robinyoon-dev.js new file mode 100644 index 0000000000..42a372f35f --- /dev/null +++ b/container-with-most-water/robinyoon-dev.js @@ -0,0 +1,32 @@ +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function (height) { + + // 투 포인터 + // 왼쪽 포인터, 오른쪽 포인터. lower 라인의 포인터를 움직여야함. + + let leftPointer = 0; + let rightPointer = height.length - 1; + let tempMax = 0; + + while (leftPointer !== rightPointer) { + let areaHeight = Math.min(height[leftPointer], height[rightPointer]); + + let areaWidth = rightPointer - leftPointer; + + let waterArea = areaHeight * areaWidth; + + tempMax = Math.max(tempMax, waterArea); + + if (height[leftPointer] < height[rightPointer]) { + leftPointer++; + } else { + rightPointer--; + } + } + + return tempMax; + +}; diff --git a/valid-parentheses/robinyoon-dev.js b/valid-parentheses/robinyoon-dev.js index 5dc3b6754d..cf45473046 100644 --- a/valid-parentheses/robinyoon-dev.js +++ b/valid-parentheses/robinyoon-dev.js @@ -3,25 +3,63 @@ * @return {boolean} */ var isValid = function (s) { - - const tempArray = []; - const pairObject = { + const bracketsObj = { ')': '(', '}': '{', ']': '[' } + const stackArray = []; + + for (let i = 0; i < s.length; i++) { - for (const ch of s) { - if (ch === '(' || ch === '{' || ch === '[') { - tempArray.push(ch); + let currentBracket = s[i]; + let isRightBracket = currentBracket in bracketsObj; + + if (!isRightBracket) { + stackArray.push(currentBracket); + continue; + } + + let isPair = stackArray[stackArray.length - 1] == bracketsObj[currentBracket]; + + if (isPair) { + stackArray.pop(); } else { - if (tempArray.pop() !== pairObject[ch]) { - return false; - } + return false; } } - return tempArray.length === 0; + return stackArray.length === 0 + }; +// -----6기 활동 시의 풀이---- +// /** +// * @param {string} s +// * @return {boolean} +// */ +// var isValid = function (s) { + +// const tempArray = []; +// const pairObject = { +// ')': '(', +// '}': '{', +// ']': '[' +// } + +// for (const ch of s) { +// if (ch === '(' || ch === '{' || ch === '[') { +// tempArray.push(ch); +// } else { +// if (tempArray.pop() !== pairObject[ch]) { +// return false; +// } +// } +// } + +// return tempArray.length === 0; +// }; + + +