From 024d29c175cbdc74782015573096842d30ae4f49 Mon Sep 17 00:00:00 2001 From: eminencegrs Date: Tue, 12 Nov 2024 21:39:39 +0100 Subject: [PATCH] GenerateParentheses: add examples with explanations. --- .../ExampleWithExplanation.md | 34 ----- .../ExamplesWithExplanations.md | 125 ++++++++++++++++++ 2 files changed, 125 insertions(+), 34 deletions(-) delete mode 100644 LeetCode/src/LeetCode.Challenges/GenerateParentheses/ExampleWithExplanation.md create mode 100644 LeetCode/src/LeetCode.Challenges/GenerateParentheses/ExamplesWithExplanations.md diff --git a/LeetCode/src/LeetCode.Challenges/GenerateParentheses/ExampleWithExplanation.md b/LeetCode/src/LeetCode.Challenges/GenerateParentheses/ExampleWithExplanation.md deleted file mode 100644 index 15c150d..0000000 --- a/LeetCode/src/LeetCode.Challenges/GenerateParentheses/ExampleWithExplanation.md +++ /dev/null @@ -1,34 +0,0 @@ -# Example: 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) -''' \ No newline at end of file diff --git a/LeetCode/src/LeetCode.Challenges/GenerateParentheses/ExamplesWithExplanations.md b/LeetCode/src/LeetCode.Challenges/GenerateParentheses/ExamplesWithExplanations.md new file mode 100644 index 0000000..b7eced2 --- /dev/null +++ b/LeetCode/src/LeetCode.Challenges/GenerateParentheses/ExamplesWithExplanations.md @@ -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) +```