Skip to content

Commit 8589f3e

Browse files
Update increasing-subsequences.java
1 parent e04acb8 commit 8589f3e

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

increasing-subsequences.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
public class Solution {
22
public List<List<Integer>> findSubsequences(int[] nums) {
33
int n = nums.length;
4-
List<List<Integer>> ans = new ArrayList<List<Integer>>();
5-
for (int i = 0; i < n; i++) {
6-
for (int len = 2; len <= n - i; len++) {
7-
int pivot = 1, int index = i;
8-
List<Integer> temp = new ArrayList<Integer>();
9-
temp.add(nums[index++]);
10-
while (index < n && pivot < len) {
11-
if (nums[index] > nums[index-1]) {
12-
temp.add(nums[index]);
13-
pivot++;
14-
}
15-
index++;
16-
}
17-
ans.add(temp);
4+
Set<List<Integer>> ans = new HashSet<List<Integer>>();
5+
List<Integer> temp = new ArrayList<Integer>();
6+
findSubsequencesHelper(nums, ans, temp, 0);
7+
return new ArrayList(ans);
8+
}
9+
public void findSubsequencesHelper(int[] nums, Set<List<Integer>> ans, List<Integer> temp, int index) {
10+
if (temp.size() >= 2) ans.add(new ArrayList(temp));
11+
for (int i = index; i < nums.length; i++) {
12+
if (temp.size() == 0 || temp.get(temp.size()-1) <= nums[i]) {
13+
temp.add(nums[i]);
14+
findSubsequencesHelper(nums, ans, temp, index+1);
15+
temp.remove(temp.size()-1);
1816
}
1917
}
20-
return ans;
2118
}
2219
}

0 commit comments

Comments
 (0)