生成括号时空复杂度

问题描述

这个问题与:https://leetcode.com/problems/generate-parentheses

Given n pairs of parentheses,write a function to generate all combinations of well-formed parentheses.

我使用回溯解决了这个问题,但无法理解解决方案中给出的时间和空间复杂性。这是我为 n = 2 绘制的递归树

                  ''
                  |
                 '('    
               /     \
            '(('      '()'
              |         | 
            '(()'     '()('    
              |         |
            '(())'    '()()'




这是我的解决方案:

class Solution:
    def generateParenthesis(self,n: int) -> List[str]:
        
        N = 2 * n
        results = []
        
        def gen(S,left,right):
            if len(S) == N:
                results.append(S)
            else:
                if left < n:
                    gen(S+'(',left+1,right)
                if right < left:
                    gen(S+')',right+1)

        gen("",0)
        return results

解决方案提到了时间和空间复杂性的加泰罗尼亚数:

enter image description here

我不仅不理解这些,我对时间和空间复杂性的直觉也没有超出“它至少是线性的”。

你能帮我理解吗?

非常感谢!!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...