-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path21_November.java
More file actions
32 lines (28 loc) · 812 Bytes
/
Copy path21_November.java
File metadata and controls
32 lines (28 loc) · 812 Bytes
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
//User function Template for Java
class Solution
{
public ArrayList<ArrayList<Integer>> UniquePartitions(int n)
{
// Code here
ArrayList<ArrayList<Integer>> ans=new ArrayList<>();
ArrayList<Integer> curr=new ArrayList<>();
int idx=n-1;
int[]arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=i+1;
}
dfs(ans,curr,idx,n,arr);
return ans;
}
void dfs(ArrayList<ArrayList<Integer>> ans,ArrayList<Integer> curr,
int idx,int n,int[]arr){
if(idx<0 || n<0)return;
curr.add(arr[idx]);
if(n-arr[idx]==0){
ans.add(new ArrayList<>(curr));
}
dfs(ans,curr,idx,n-arr[idx],arr);
curr.remove(curr.size()-1);
dfs(ans,curr,idx-1,n,arr);
}
}