From f9c2c88398252b6391227af5ba416a22bafc3271 Mon Sep 17 00:00:00 2001 From: Aerex Date: Sun, 8 Aug 2021 10:53:32 -0500 Subject: [PATCH] [nb] Commit --- .../22-generate-parenthesis/golang/main.go | 55 +++++++++---------- .../22-generate-parenthesis/js/index.js | 41 +++++++++----- .../22-generate-parenthesis/python/index.py | 25 +++++++++ 3 files changed, 78 insertions(+), 43 deletions(-) create mode 100644 dynamic-programming/22-generate-parenthesis/python/index.py diff --git a/dynamic-programming/22-generate-parenthesis/golang/main.go b/dynamic-programming/22-generate-parenthesis/golang/main.go index 8a0178d..ea57f74 100644 --- a/dynamic-programming/22-generate-parenthesis/golang/main.go +++ b/dynamic-programming/22-generate-parenthesis/golang/main.go @@ -1,4 +1,8 @@ package main +import ( + "strings" + "container/list" +) /* Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example 1: @@ -20,37 +24,30 @@ Constraints: 1 <= n <= 8 */ -func rec(int index, dp map[string]string) { +func rec(input []string, left int, right int, n int, ans *[]string) { + + if len(input) == 2*n { + *ans = append(*ans, strings.Join(input, "")) + return + } + if left < n { + input = append(input, "(") + rec(input, left+1, right, n, ans) + input = input[0:len(input)-1] + } + if right < left { + input = append(input, ")") + rec(input, left, right+1, n, ans) + input = input[0:len(input)-1] + } } - -func generateParenthesis(n int)[] string { - - if n == 1 { - return []string{"()"} - } - - if n == 2 { - return []string{"()()", "(())"} - } - - var output = make([]string, 0) - var dp = map[string]string{} - var start_string string = "" - for i := 0; i < n - 1; i++ { - start_string = start_string + "()" - out_arr = a - } - output = append(output, start_string) - - - for i := 1; i < n - 1; i++ { - result := rec(i+1, dp) + rec(i-1) - } - - +func generateParenthesis(n int) []string { + var output = make([]string, 2*n) + var input = make([]string, 2*n) + rec(input, 0, 0, n, &output) + return output } - func main() { - + generateParenthesis(3) } diff --git a/dynamic-programming/22-generate-parenthesis/js/index.js b/dynamic-programming/22-generate-parenthesis/js/index.js index 76d8e9a..e6274e9 100644 --- a/dynamic-programming/22-generate-parenthesis/js/index.js +++ b/dynamic-programming/22-generate-parenthesis/js/index.js @@ -1,17 +1,30 @@ function generateParenthesis(n) { - if (n == 1) { - return ['()']; - } + const output = [] + const rec = (input = [], left = 0, right = 0) => { + if (input.length === 2*n) { + output.push(input.join('')); + console.log('sol'); + return + } + if (left < n) { + input.push('('); + console.log(input.join('') + ' l=' + left + ' r=' + right) + rec(input, left+1, right); + console.log('back left'); + input.pop(); + } - if (n == 2) { - return ['()()', '(())']; - } - - const input = []; - for (i = 0; i < n; i++) { - input.push('()'); - } - - for(i = 0; i < n; i++; i++) { - } + if (right < left) { + input.push(')'); + console.log(input.join('') + ' l=' + left + ' r=' + right) + rec(input, left, right+1); + console.log('back right l=' + left); + input.pop(); + console.log(input.join('') + ' l=' + left + ' r=' + right) + } + }; + rec(); + return output; } + +console.log(generateParenthesis(3)); diff --git a/dynamic-programming/22-generate-parenthesis/python/index.py b/dynamic-programming/22-generate-parenthesis/python/index.py new file mode 100644 index 0000000..7a70030 --- /dev/null +++ b/dynamic-programming/22-generate-parenthesis/python/index.py @@ -0,0 +1,25 @@ +from typing import List +# ()()() +# (())() +# ((())) +# ()(()) +# (()()) + +def generateParenthesis(n: int) -> List[str]: + ans = [] + def backtrack(S = [], left = 0, right = 0): + if len(S) == 2 * n: + ans.append("".join(S)) + return + if left < n: + S.append("(") + backtrack(S, left+1, right) + S.pop() + if right < left: + S.append(")") + backtrack(S, left, right+1) + S.pop() + backtrack() + return ans + +generateParenthesis(3)