Skip to content

[dd_._._bb] Week 6 #893

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

Merged
merged 4 commits into from
Jan 19, 2025
Merged
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
41 changes: 41 additions & 0 deletions container-with-most-water/lledellebell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/***
*
* @problem
* - 배열에서 두 선을 선택하여 최대 물을 담을 수 있는 면적을 구하는 문제
*
* @constraints
* - 배열의 길이는 2 이상 10^5 이하입니다.
* - 각 높이는 0 이상 10^4 이하의 정수입니다.
*
* @example
* - 입력: height = [1,8,6,2,5,4,8,3,7]
* - 출력: 49
* (높이 8과 7을 선택하여 넓이 = min(8, 7) * (8 - 1) = 49)
*
* @description
* - 배열의 양 끝에서 시작하는 두 개의 포인터를 사용하여 문제를 해결합니다.
* - 현재 포인터 위치에서의 넓이를 계산하고 최대값을 갱신합니다.
* - 더 작은 높이를 가진 포인터를 이동하여 더 큰 넓이를 탐색합니다.
*
* @complexity
* - 시간 복잡도: O(n)
* (배열을 한 번만 순회하며 최대 넓이를 계산)
* - 공간 복잡도: O(1)
* (추가 메모리 사용 없이 포인터만 사용)
*
*/
function maxArea(height: number[]): number {
let left = 0,
right = height.length - 1,
maxArea = 0;

while (left < right) {
// 현재 넓이 계산 및 최대값 갱신
maxArea = Math.max(maxArea, Math.min(height[left], height[right]) * (right - left));

// 더 작은 높이의 포인터를 이동
height[left] < height[right] ? left++ : right--;
}

return maxArea;
}
67 changes: 67 additions & 0 deletions valid-parentheses/lledellebell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/***
*
* @problem
* - 여는 괄호와 닫는 괄호가 올바르게 짝을 이루는지 확인하는 문제입니다.
*
* @constraints
* - 문자열은 오직 '(', ')', '{', '}', '[', ']' 문자로만 구성됩니다.
* - 문자열의 길이는 0 이상 10^4 이하입니다.
*
* @example
* - 입력: "()"
* 출력: true
* - 입력: "(){}"
* 출력: true
* - 입력: "(]"
* 출력: false
* - 입력: "([)]"
* 출력: false
* - 입력: "{}"
* 출력: true
*
* @description
* - 여는 괄호는 스택에 추가하고, 닫는 괄호가 나오면 스택의 맨 위 요소(가장 최근에 추가된 여는 괄호)와 비교합니다.
* - 닫는 괄호와 스택의 맨 위 요소가 짝을 이루지 않으면 유효하지 않은 문자열로 판단합니다.
* - 문자열을 모두 순회한 후 스택이 비어 있으면 유효한 괄호 문자열입니다.
*
* @complexity
* - 시간 복잡도: O(n)
* 문자열을 한 번만 순회하므로 시간 복잡도는 O(n)입니다.
* - 공간 복잡도: O(n)
* 스택에 저장되는 여는 괄호의 최대 개수는 문자열 길이에 비례하므로 공간 복잡도는 O(n)입니다.
*
*/
function isValid(s: string): boolean {
// 괄호 쌍을 저장하는 Map 생성
const map = new Map<string, string>([
[')', '('],
['}', '{'],
[']', '[']
]);

// 스택 초기화
const stack: string[] = [];

// 문자열을 순회
for (const char of s) {
if (map.has(char)) {
// 닫는 괄호일 경우 스택에서 pop하여 비교
const topElement = stack.length > 0 ? stack.pop() : undefined;
if (topElement !== map.get(char)) {
return false; // 올바르지 않은 경우
}
} else {
// 여는 괄호일 경우 스택에 push
stack.push(char);
}
}

// 스택이 비어 있으면 유효한 괄호(모든 여는 괄호가 닫는 괄호와 짝을 이룸)
return stack.length === 0;
}

console.log(isValid("()")); // true
console.log(isValid("(){}")); // true
console.log(isValid("(]")); // false
console.log(isValid("([)]")); // false
console.log(isValid("{[]}")); // true
Loading