-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathIncreasing Subsequences.java
More file actions
38 lines (30 loc) · 947 Bytes
/
Increasing Subsequences.java
File metadata and controls
38 lines (30 loc) · 947 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
33
34
35
36
37
38
class Solution {
Set<List<Integer>> ans;
public List<List<Integer>> findSubsequences(int[] nums) {
ans = new HashSet<>();
helper(nums, 0, new ArrayList<>());
return new ArrayList<>(ans);
}
private void helper(int[] nums, int start, List<Integer> list) {
if (start == nums.length) {
if (list.size() >= 2) {
ans.add(new ArrayList<>(list));
return;
}
}
if (list.size() >= 2) {
ans.add(new ArrayList<>(list));
}
for (int idx = start; idx < nums.length; idx++) {
if (list.size() != 0 && nums[idx] < list.get(list.size() - 1)) {
continue;
}
// Choose
list.add(nums[idx]);
// Explore
helper(nums, idx + 1, list);
// Un-choose
list.remove(list.size() - 1);
}
}
}