wherehows
Home
  • 분류 전체보기 (35)
    • 알고리즘 (31)
      • 코드트리 (0)
      • 백준 (1)
      • 리트코드 (21)
      • 프로그래머스 (9)
Home
  • 분류 전체보기 (35)
    • 알고리즘 (31)
      • 코드트리 (0)
      • 백준 (1)
      • 리트코드 (21)
      • 프로그래머스 (9)
블로그 내 검색

wherehows

  • 알고리즘/리트코드

    856. Score of Parentheses

    2021. 7. 14.

    by. 창고

    https://leetcode.com/problems/score-of-parentheses/discuss/141777/C%2B%2BJavaPython-O(1)-Space 

     

    [C++/Java/Python] O(1) Space - LeetCode Discuss

    Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

    leetcode.com

    내 풀이는 의미가 없다. lee215씨의 코드를 보자.

     

    https://leetcode.com/problems/score-of-parentheses/discuss/141777/C%2B%2BJavaPython-O(1)-Space 

     

    [C++/Java/Python] O(1) Space - LeetCode Discuss

    Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

    leetcode.com

    잘 이해가 안간다. 실제로 케이스를 넣고 하나하나 따라가보니 문제에서 요구하는 해답을 구하는건 알겠지만, 어떻게 이런 생각을 했는지는 잘 모르겠다.

     

    function scoreOfParentheses(s) {
        let stack = [];
        let cur = 0;
        for(let i = 0; i < s.length; i++) {
            if (s[i] == '(') {
                stack.push(cur);
                cur = 0;
            } else {
                cur = stack.pop() + Math.max(cur * 2, 1);
            }
        }
        return cur;
    }

    '알고리즘 > 리트코드' 카테고리의 다른 글

    209. Minimum Size Subarray Sum  (0) 2021.07.18
    47. Permutations II  (1) 2021.06.20
    137. Single Number II  (0) 2021.06.19
    287. Find the Duplicate Number  (0) 2021.06.18
    153. Find Minimum in Rotated Sorted Array  (0) 2021.06.17

    댓글

    관련글

    • 209. Minimum Size Subarray Sum 2021.07.18
    • 47. Permutations II 2021.06.20
    • 137. Single Number II 2021.06.19
    • 287. Find the Duplicate Number 2021.06.18
    맨 위로
전체 글 보기
Tistory 로그인
Tistory 로그아웃
로그아웃 글쓰기 관리

Today

Total

Powered by ⓒ Kakao Corp.

Designed by Nana
블로그 이미지
창고

티스토리툴바