[LeetCode] 1614. Maximum Nesting Depth of the Parentheses

Chobby·2025년 9월 24일
1

LeetCode

목록 보기
560/582

😎풀이

  1. 현재 깊이 정의
  2. 최대 깊이 정의
  3. s 순회
    3-1. 여는 괄호라면, 현재 깊이 추가 및 최대 깊이 갱신
    3-2. 닫는 괄호라면, 현재 깊이 감소
  4. 최대 깊이 반환환
function maxDepth(s: string): number {
    let curDepth = 0
    let maxDepth = 0
    for(const char of s) {
        if(char === '(') {
            curDepth++
            maxDepth = Math.max(maxDepth, curDepth)
        } else if(char === ')') {
            curDepth--
        }
    }
    return maxDepth
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글