-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GenerateParentheses: add examples with explanations.
1 parent
efb059f
commit 024d29c
Showing
2 changed files
with
125 additions
and
34 deletions.
There are no files selected for viewing
34 changes: 0 additions & 34 deletions
34
LeetCode/src/LeetCode.Challenges/GenerateParentheses/ExampleWithExplanation.md
This file was deleted.
Oops, something went wrong.
125 changes: 125 additions & 0 deletions
125
LeetCode/src/LeetCode.Challenges/GenerateParentheses/ExamplesWithExplanations.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
## Examples with explanations | ||
|
||
### Example 1 | ||
|
||
Input: `n = 2` | ||
|
||
**Explanation:** | ||
|
||
``` | ||
GenerateParenthesis(2) | ||
└── Handle("", 0, 0, 2) | ||
| | ||
├── "(" added → Handle("(", 1, 0, 2) | ||
| | | ||
| ├── "(" added → Handle("((", 2, 0, 2) | ||
| | | | ||
| | ├── ")" added → Handle("(()", 2, 1, 2) | ||
| | | | | ||
| | | ├── ")" added → Handle("(())", 2, 2, 2) <-- [Base case reached, add "(())" to result] | ||
| | | | | ||
| | | ← Backtrack to Handle("(()", 2, 1, 2) | ||
| | | | ||
| | ← Backtrack to Handle("((", 2, 0, 2) | ||
| | | ||
| ← Backtrack to Handle("(", 1, 0, 2) | ||
| | ||
├── ")" added → Handle("()", 1, 1, 2) | ||
| | | ||
| ├── "(" added → Handle("()(", 2, 1, 2) | ||
| | | | ||
| | ├── ")" added → Handle("()()", 2, 2, 2) <-- [Base case reached, add "()()" to result] | ||
| | | | ||
| | ← Backtrack to Handle("()(", 2, 1, 2) | ||
| | | ||
| ← Backtrack to Handle("()", 1, 1, 2) | ||
| | ||
← Backtrack to Handle("", 0, 0, 2) | ||
``` | ||
|
||
### Example 2 | ||
|
||
Input: `n = 3` | ||
|
||
**Explanation:** | ||
|
||
``` | ||
GenerateParenthesis(3) | ||
└── Handle("", 0, 0, 3) | ||
| | ||
├── "(" added → Handle("(", 1, 0, 3) | ||
| | | ||
| ├── "(" added → Handle("((", 2, 0, 3) | ||
| | | | ||
| | ├── "(" added → Handle("(((", 3, 0, 3) | ||
| | | | | ||
| | | ├── ")" added → Handle("((()", 3, 1, 3) | ||
| | | | | | ||
| | | | ├── ")" added → Handle("((())", 3, 2, 3) | ||
| | | | | | | ||
| | | | | ├── ")" added → Handle("((()))", 3, 3, 3) ← Base case, add "((()))" to result | ||
| | | | | | | ||
| | | | | ← Backtrack to Handle("((())", 3, 2, 3) | ||
| | | | | | ||
| | | | ← Backtrack to Handle("((()", 3, 1, 3) | ||
| | | | | ||
| | | ← Backtrack to Handle("(((", 3, 0, 3) | ||
| | | | ||
| | ├── ")" added → Handle("(()", 2, 1, 3) | ||
| | | | | ||
| | | ├── "(" added → Handle("(()(", 3, 1, 3) | ||
| | | | | | ||
| | | | ├── ")" added → Handle("(()()", 3, 2, 3) | ||
| | | | | | | ||
| | | | | ├── ")" added → Handle("(()())", 3, 3, 3) ← Base case, add "(()())" to result | ||
| | | | | | | ||
| | | | | ← Backtrack to Handle("(()()", 3, 2, 3) | ||
| | | | | | ||
| | | | ← Backtrack to Handle("(()(", 3, 1, 3) | ||
| | | | | ||
| | | ← Backtrack to Handle("(()", 2, 1, 3) | ||
| | | | ||
| | ├── ")" added → Handle("(())", 2, 2, 3) | ||
| | | | | ||
| | | ├── "(" added → Handle("(())(", 3, 2, 3) | ||
| | | | | | ||
| | | | ├── ")" added → Handle("(())()", 3, 3, 3) ← Base case, add "(())()" to result | ||
| | | | | | ||
| | | | ← Backtrack to Handle("(())(", 3, 2, 3) | ||
| | | | | ||
| | | ← Backtrack to Handle("(())", 2, 2, 3) | ||
| | | | ||
| | ← Backtrack to Handle("((", 2, 0, 3) | ||
| | | ||
| ← Backtrack to Handle("(", 1, 0, 3) | ||
| | ||
├── ")" added → Handle("()", 1, 1, 3) | ||
| | | ||
| ├── "(" added → Handle("()(", 2, 1, 3) | ||
| | | | ||
| | ├── "(" added → Handle("()((", 3, 1, 3) | ||
| | | | | ||
| | | ├── ")" added → Handle("()(()", 3, 2, 3) | ||
| | | | | | ||
| | | | ├── ")" added → Handle("()(())", 3, 3, 3) ← Base case, add "()(())" to result | ||
| | | | | | ||
| | | | ← Backtrack to Handle("()(()", 3, 2, 3) | ||
| | | | | ||
| | | ← Backtrack to Handle("()((", 3, 1, 3) | ||
| | | | ||
| | ├── ")" added → Handle("()()", 2, 2, 3) | ||
| | | | | ||
| | | ├── "(" added → Handle("()()(", 3, 2, 3) | ||
| | | | | | ||
| | | | ├── ")" added → Handle("()()()", 3, 3, 3) ← Base case, add "()()()" to result | ||
| | | | | | ||
| | | | ← Backtrack to Handle("()()(", 3, 2, 3) | ||
| | | | | ||
| | | ← Backtrack to Handle("()()", 2, 2, 3) | ||
| | | | ||
| | ← Backtrack to Handle("()(", 2, 1, 3) | ||
| | | ||
| ← Backtrack to Handle("()", 1, 1, 3) | ||
| | ||
← Backtrack to Handle("", 0, 0, 3) | ||
``` |