Skip to content

[uarflower] WEEK 06 solutions #1424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions container-with-most-water/uraflower.js
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 30 additions & 0 deletions valid-parentheses/uraflower.js
Original file line number Diff line number Diff line change
@@ -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)