-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0491IncreasingSubsequences.java
More file actions
71 lines (61 loc) · 2.06 KB
/
_0491IncreasingSubsequences.java
File metadata and controls
71 lines (61 loc) · 2.06 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.heatwave.leetcode.problems;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class _0491IncreasingSubsequences {
static class Solution {
List<Integer> temp = new ArrayList<>();
Set<List<Integer>> set = new HashSet<>();
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
backtrack(nums, 0);
return ans;
}
private void backtrack(int[] nums, int index) {
if (temp.size() > 1) {
ArrayList<Integer> list = new ArrayList<>(temp);
if (!set.contains(list)) {
ans.add(list);
set.add(list);
}
}
for (int i = index; i < nums.length; i++) {
if (temp.size() > 0 && nums[i] < temp.get(temp.size() - 1)) {
continue;
}
temp.add(nums[i]);
backtrack(nums, i + 1);
temp.remove(temp.size() - 1);
}
}
}
static class AnotherSolution {
List<Integer> temp = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
dfs(0, Integer.MIN_VALUE, nums);
return ans;
}
private void dfs(int cur, int last, int[] nums) {
if (cur == nums.length) {
if (temp.size() >= 2) {
ans.add(new ArrayList<>(temp));
}
return;
}
if (nums[cur] >= last) {
temp.add(nums[cur]);
dfs(cur + 1, nums[cur], nums);
temp.remove(temp.size() - 1);
}
if (nums[cur] != last) {
dfs(cur + 1, last, nums);
}
}
}
public static void main(String[] args) {
Solution solution = new Solution();
solution.findSubsequences(new int[]{1, 2, 3, 1, 1, 1});
}
}