목차

Generate Parentheses

ps
링크leetcode.com/…
출처LeetCode
문제 번호22
문제명Generate Parentheses
레벨Medium
분류

완전탐색

시간복잡도O(4^n / sqrt(n))
인풋사이즈n <= 8
사용한 언어Python
제출기록28 ms / 14.4 MB
최고기록12ms
해결날짜2020/11/26

풀이

코드

"""Solution code for "LeetCode 22. Generate Parentheses".

- Problem link: https://leetcode.com/problems/generate-parentheses/
- Solution link: http://www.teferi.net/ps/problems/leetcode/22
"""


class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        par_lists = [[] for _ in range(n + 1)]
        par_lists[0] = ['']
        for i in range(1, n + 1):
            for j in range(i):
                left_list = par_lists[j]
                right_list = par_lists[i - j - 1]
                for l in left_list:
                    par = '(' + l + ')'
                    par_lists[i].extend([par + r for r in right_list])

        return par_lists[n]