From c35240d871cb560a809b0d6efec9b1d84275be9f Mon Sep 17 00:00:00 2001 From: rossy0213 Date: Tue, 4 Jun 2024 00:54:24 +0900 Subject: [PATCH] Generate Parentheses --- GenerateParentheses/step1.md | 30 ++++++++++++++ GenerateParentheses/step2.md | 25 ++++++++++++ GenerateParentheses/step3.md | 77 ++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 GenerateParentheses/step1.md create mode 100644 GenerateParentheses/step2.md create mode 100644 GenerateParentheses/step3.md diff --git a/GenerateParentheses/step1.md b/GenerateParentheses/step1.md new file mode 100644 index 0000000..081cd78 --- /dev/null +++ b/GenerateParentheses/step1.md @@ -0,0 +1,30 @@ +```java +// Until reach to the max length(n * 2) +// We try to add left part first then add right side +// Until number of left part reach to n, we can add left part +// And if number of left part is bigger than right part, we can try to add right part +// Time complexity: O(2**(N*2)) +// Space complexity: O(n) stack memory for backtack +// Time spend: XX(Found the answer after checking Solutions tab) +class Solution { + public List generateParenthesis(int n) { + List res = new ArrayList<>(); + generateParenthesis(res, 0, 0, "", n); + return res; + } + + public void generateParenthesis(List res, int l, int r, String curr,int n) { + if (curr.length() == n * 2) { + res.add(curr); + return; + } + + if (l < n) { + generateParenthesis(res, l + 1, r, curr + "(", n); + } + if (r < l) { + generateParenthesis(res, l, r + 1, curr + ")", n); + } + } +} +``` \ No newline at end of file diff --git a/GenerateParentheses/step2.md b/GenerateParentheses/step2.md new file mode 100644 index 0000000..492e41f --- /dev/null +++ b/GenerateParentheses/step2.md @@ -0,0 +1,25 @@ +```java +// Time spend: 01:47 +class Solution { + public List generateParenthesis(int n) { + List res = new ArrayList<>(); + generateParenthesis(res, 0, 0, n, ""); + return res; + } + + public void generateParenthesis(List res, int l, int r, int n, String curr) { + if (curr.length() == n * 2) { + res.add(curr); + return; + } + + if (l < n) { + generateParenthesis(res, l + 1, r, n, curr + '('); + } + + if (r < l) { + generateParenthesis(res, l, r + 1, n, curr + ')'); + } + } +} +``` \ No newline at end of file diff --git a/GenerateParentheses/step3.md b/GenerateParentheses/step3.md new file mode 100644 index 0000000..b71884a --- /dev/null +++ b/GenerateParentheses/step3.md @@ -0,0 +1,77 @@ +```java +// Time spend: 01:33 +class Solution { + public List generateParenthesis(int n) { + List res = new ArrayList<>(); + generateParenthesis(res, 0, 0, n, ""); + return res; + } + + public void generateParenthesis(List res, int l, int r, int n, String curr) { + if (curr.length() == n * 2) { + res.add(curr); + return; + } + + if (l < n) { + generateParenthesis(res, l + 1, r, n, curr + '('); + } + + if (r < l) { + generateParenthesis(res, l, r + 1, n, curr + ')'); + } + } +} +``` + +```java +// Time spend: 01:37 +class Solution { + public List generateParenthesis(int n) { + List res = new ArrayList<>(); + generateParenthesis(res, 0, 0, n, ""); + return res; + } + + public void generateParenthesis(List res, int l, int r, int n, String curr) { + if (curr.length() == n * 2) { + res.add(curr); + return; + } + + if (l < n) { + generateParenthesis(res, l + 1, r, n, curr + '('); + } + + if (r < l) { + generateParenthesis(res, l, r + 1, n, curr + ')'); + } + } +} +``` + +```java +// Time spend: 01:26 +class Solution { + public List generateParenthesis(int n) { + List res = new ArrayList<>(); + generateParenthesis(res, 0, 0, n, ""); + return res; + } + + public void generateParenthesis(List res, int l, int r, int n, String curr) { + if (curr.length() == n * 2) { + res.add(curr); + return; + } + + if (l < n) { + generateParenthesis(res, l + 1, r, n, curr + '('); + } + + if (r < l) { + generateParenthesis(res, l, r + 1, n, curr + ')'); + } + } +} +``` \ No newline at end of file