mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-25 21:58:57 +01:00
Add complexity analysis and detailed example
This commit is contained in:
parent
1d974b2ea1
commit
7eea20ed6e
1 changed files with 49 additions and 0 deletions
|
@ -25,6 +25,14 @@
|
|||
# And we could start a closing bracket if it'd not exceed the number of opening
|
||||
# brackets.
|
||||
|
||||
# Complexity Analysis
|
||||
#
|
||||
# Time Complexity: O(4^n/sqrt(n). Each valid sequence has at most n steps during the backtracking procedure.
|
||||
# Space Complexity: O(4^n/sqrt(n), as described above, and using O(n) space to store the sequence.
|
||||
|
||||
# Refer to the attached diagram for recursion,
|
||||
# The numbers next to each node are the counts of left and right parantheses
|
||||
|
||||
# @param {Integer} n
|
||||
# @return {String[]}
|
||||
def generate_parenthesis(n)
|
||||
|
@ -52,6 +60,47 @@ n = 3
|
|||
print(generate_parenthesis(n))
|
||||
# Output: ["((()))","(()())","(())()","()(())","()()()"]
|
||||
|
||||
# *** Example: n = 3 *** Space after each DFS instance
|
||||
# backtrack called with 0 0 []
|
||||
# backtrack called with ( 1 0 []
|
||||
# backtrack called with (( 2 0 []
|
||||
# backtrack called with ((( 3 0 []
|
||||
# backtrack called with ((() 3 1 []
|
||||
# backtrack called with ((()) 3 2 []
|
||||
# backtrack called with ((())) 3 3 []
|
||||
# backtrack return with ((()) 3 2 ["((()))"]
|
||||
# backtrack return with ((() 3 1 ["((()))"]
|
||||
# backtrack return with ((( 3 0 ["((()))"]
|
||||
# backtrack called with (() 2 1 ["((()))"]
|
||||
# backtrack called with (()( 3 1 ["((()))"]
|
||||
# backtrack called with (()() 3 2 ["((()))"]
|
||||
# backtrack called with (()()) 3 3 ["((()))"]
|
||||
# backtrack return with (()() 3 2 ["((()))", "(()())"]
|
||||
# backtrack return with (()( 3 1 ["((()))", "(()())"]
|
||||
# backtrack called with (()) 2 2 ["((()))", "(()())"]
|
||||
# backtrack called with (())( 3 2 ["((()))", "(()())"]
|
||||
# backtrack called with (())() 3 3 ["((()))", "(()())"]
|
||||
# backtrack return with (())( 3 2 ["((()))", "(()())", "(())()"]
|
||||
# backtrack return with (()) 2 2 ["((()))", "(()())", "(())()"]
|
||||
# backtrack return with (() 2 1 ["((()))", "(()())", "(())()"]
|
||||
# backtrack return with (( 2 0 ["((()))", "(()())", "(())()"]
|
||||
# backtrack called with () 1 1 ["((()))", "(()())", "(())()"]
|
||||
# backtrack called with ()( 2 1 ["((()))", "(()())", "(())()"]
|
||||
# backtrack called with ()(( 3 1 ["((()))", "(()())", "(())()"]
|
||||
# backtrack called with ()(() 3 2 ["((()))", "(()())", "(())()"]
|
||||
# backtrack called with ()(()) 3 3 ["((()))", "(()())", "(())()"]
|
||||
# backtrack return with ()(() 3 2 ["((()))", "(()())", "(())()", "()(())"]
|
||||
# backtrack return with ()(( 3 1 ["((()))", "(()())", "(())()", "()(())"]
|
||||
# backtrack called with ()() 2 2 ["((()))", "(()())", "(())()", "()(())"]
|
||||
# backtrack called with ()()( 3 2 ["((()))", "(()())", "(())()", "()(())"]
|
||||
# backtrack called with ()()() 3 3 ["((()))", "(()())", "(())()", "()(())"]
|
||||
# backtrack return with ()()( 3 2 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
# backtrack return with ()() 2 2 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
# backtrack return with ()( 2 1 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
# backtrack return with () 1 1 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
# backtrack return with ( 1 0 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
# backtrack return with 0 0 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
|
||||
n = 1
|
||||
print(generate_parenthesis(n))
|
||||
# Output: ["()"]
|
||||
|
|
Loading…
Reference in a new issue