leetcode/22_generate_parentheses
22_generate_parentheses
dz /
leetcode /
22_generate_parentheses
Summary
22. Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Node Tree
Nodes
brute_force
|
content
|
There are 2^N total combinations to choose, a maximum of 256 iterations. You could produce every combination, some way of checking if it is properly closed, then adding it to an array if it is.
|
parents
|
intuition
|
generate_first_half
|
content
|
I wonder if there's any effeciency involved in generating the first half of sequence, then working with the other half?
|
parents
|
intuition
|
recursion
|
content
|
It seems like this could be a solution you could express recursively. In the example, it was very clear that at the end there were 3 small pairs LRLRLR, and at the those pairs were nested.
|
children
|
dynamic_programming (follow-up, inspired by seeing smaller things grow into)
|
parents
|
intuition, n_3
|
dynamic_programming
|
content
|
larger things Oh, I see how things could be broken up into smaller problems. With a function GP(n), and the operator GP(x)*GP(y) concatenates all the combinations for inside of GP(x) and GP(y) with X followed by Y. would can see how when N=0, the output is [] when N=1, the output is [LR], or [L*GP(0)*R, GP(0)*LR, LR*GP(0)]. when N=2, the combinations are [GP(1)*GP(1),LGP(1)R]. When N=3, the combinations are all of 2 plus an extra pair: GP(2)*LR, LR*GP(2), L*GP(2)*R. Which can be generalized as [GP(N-1)*LR, LR*GP(N-1), LGP(N-1)]. This notation produces duplicates though. I think it's close to a more elegant solution than brute force.
|
children
|
divide_and_conquer (similar line of thinking)
|
parents
|
intuition, recursion
|
brute_force_with_queue
|
content
|
The brute force version uses a queue. I don't have much practical experience working with queue data structures, so it'd be good to write this one out a language that has them trivially. I did however, get the gist of this, just not how to implement it.
|
parents
|
editorial
|
valid_only_equal_left_right
|
content
|
Is it safe to say that if a sequence has an equal number of left/right parens, it is guaranteed to be balanced? That'd be an interesting one to prove.
|
parents
|
backtracking
|
divide_and_conquer
|
content
|
The problem of generating all well-formed parens of length 2n can be decomposed into smaller subproblems of generating valid strings of smaller lengths. I was on the right track, but couldn't get past the duplicate calculations because I wasn't setting it up right.
|
parents
|
editorial, dynamic_programming
|