-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateParentheses.java
44 lines (41 loc) · 1.22 KB
/
GenerateParentheses.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Link: https://leetcode.com/problems/generate-parentheses/
// 1ms, 68.70%
class Solution {
public List<String> generateParenthesis(int n) {
List<String> ans = new ArrayList<>();
StringBuilder sb = new StringBuilder();
helper(n, 0, 0, sb, ans);
return ans;
}
private void helper(int n, int lnum, int rnum, StringBuilder cur, List<String> ans) {
if (lnum + rnum == 2 * n) {
ans.add(cur.toString());
return;
}
// try left parenthesis
if (lnum < n) {
cur.append("(");
helper(n, lnum + 1, rnum, cur, ans);
cur.deleteCharAt(cur.length() - 1);
}
// try right parenthesis
if (rnum < lnum) {
cur.append(")");
helper(n, lnum, rnum + 1, cur, ans);
cur.deleteCharAt(cur.length() - 1);
}
}
}
/*
* Time complexity: O(4^n)
* The recursion tree height is 2*n and each node has at most 2 branches. So the number
* of nodes is 2^(2n) = 4^n.
* This time complexity is not that tight, because it is impossible that every node has
* two branches (i.e., we can only try left or right parenthesis on some nodes). However
* I don't know how to get a tighter one.
*
* Space complexity: O(n)
*
* Notes:
* Combination question. We use DFS to solve it.
*/