6

The standard Generate Parenthesis question on Leetcode is as follows

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

In the solution tab they have explained Closure Number Method which I am finding it difficult to understand.

I did a dry run of the code and even got the correct answer but can't seem to understand why it works? What is the intuition behind this method?

Any help would be greatly appreciated!

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Stuxen
  • 708
  • 7
  • 21
  • 2
    If I had the time needed I would answer this, however if you Google for [Catalan Number](https://www.google.com/search?q=catalan+number&oq=cata&aqs=chrome.0.69i59j69i57j69i60l3j46.1711j0j7&sourceid=chrome&ie=UTF-8) and start reading you may find some help. Catalan Numbers are also related to [Motzkin Numbers](https://www.google.com/search?q=motzkin+number&oq=motzkin+number&aqs=chrome..69i57j69i59j69i60l2j0l2.4687j0j7&sourceid=chrome&ie=UTF-8). – Guy Coder Jun 06 '19 at 10:29
  • 2
    These concepts are fun to exploit at [Code Golf](https://codegolf.stackexchange.com/). Here is one I created related to Motzkin numbers - [Enumerate binary trees](https://codegolf.stackexchange.com/q/112874/66444) – Guy Coder Jun 06 '19 at 10:35
  • 4
    Of interest: [Catalan Numbers](http://www.geometer.org/mathcircles/catalan.pdf) by Tom Davis – Guy Coder Jun 06 '19 at 10:37
  • 3
    I added the Catalan [tag](https://stackoverflow.com/tags/catalan/info). There are many good links in the tag. – Guy Coder Jun 06 '19 at 10:38
  • @Guy Coder Thank you very much! I'll have a look at these concepts. – Stuxen Jun 06 '19 at 10:40

2 Answers2

8

The basic idea of this algorithm is dynamic programming. So you try to divide your problem into smaller problems that are easy to solve. In this example you make the sub-problems so small that the solution is either an empty string (if the size is 0) or the solution is "()" (for the size 1).

You start using the knowledge that if you want the parenthesis of a given length then the first character needs to be "(" and in some later place of the string there needs to be this character: ")". Otherwhise the output is not valid.

Now you don't know the position of the closing parenthesis so you just try every position (the first for loop).

The second thing you know, is that between the opening and the closing parenthesis and after the closing parenthesis there has to be something, that you don't realy know how it looks (because there are many possibilities), but it has to be a valid parenthesis pair again.

Now this problem is just the problem you already solved. So you just put in every possibility of valid parenthesis (using a smaller input size). Because this is just what your algorithm already does you can use the recursive function call to do this.

So summarized: You know a part of the problem, and that the rest of the problem is just the same problem with a smaller size. So you solve the small part of the problem you know and recursively call the same method to do this on the rest of the problem. Afterwards you just put it all together and got your solution.

Dynamic programming is usually not that easy to understand but very powerfull. So don't wory if you don't understand it directly. Solving puzzles like these is the best way to learn dynamic programming.

Tobias
  • 2,547
  • 3
  • 14
  • 29
1

The closure number of a sequence in the size of the smallest prefix of the sequence which is a valid sequence on its own. If a sequence has a closure number of k, than you know that in index 0 there is '(' and in index k there is ')' The method solves the problem by checking all possible sizes of such prefix, for each one it breaks the sequence to the prefix (removing the 0 and k element) and all the rest of the sequence and solving the two sub problems recursively.

Shuki Avraham
  • 1,033
  • 1
  • 7
  • 14