491. Increasing Subsequences

  • 38.9%

https://leetcode.com/problems/increasing-subsequences/description/

Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 .

1
2
3
Example:
Input: [4, 6, 7, 7]
Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]

Note:

  1. The length of the given array will not exceed 15.
  2. The range of integer in the given array is [-100,100].
  3. The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.

方法一:

我的代码实现:

值得学习,使用了set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<vector<int>> findSubsequences(vector<int>& nums) {
vector<vector<int>> paths;
vector<int> path;
dfs(nums, path, paths, 0);
return paths;
}

void dfs(vector<int>& nums, vector<int>& path, vector<vector<int>>& paths, int k){
if(path.size()>1)
paths.push_back(path);
unordered_set<int> set;
for(int i=k; i<nums.size(); i++){
if((!path.size() || nums[i]>=path.back()) && set.find(nums[i])==set.end()){
path.push_back(nums[i]);
dfs(nums, path, paths, i+1);
path.pop_back();
set.insert(nums[i]);
}
}
}
};

https://discuss.leetcode.com/topic/76274/c-dfs-solution-using-unordered_set

C++ dfs solution using unordered_set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<vector<int>> findSubsequences(vector<int>& nums) {
vector<vector<int>> res;
vector<int> seq;
dfs(res, seq, nums, 0);
return res;
}

void dfs(vector<vector<int>>& res, vector<int>& seq, vector<int>& nums, int pos) {
if(seq.size() > 1) res.push_back(seq);
unordered_set<int> hash;
for(int i = pos; i < nums.size(); ++i) {
if((seq.empty() || nums[i] >= seq.back()) && hash.find(nums[i]) == hash.end()) {
seq.push_back(nums[i]);
dfs(res, seq, nums, i + 1);
seq.pop_back();
hash.insert(nums[i]);
}
}
}
};

方法二:

https://discuss.leetcode.com/topic/76367/evolve-from-intuitive-solution-to-optimal

Evolve from intuitive solution to optimal

This is similar to Subsets II.

O(n2^n) For each number, we can either take it or drop it. Duplicates are removed by set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vector<vector<int>> findSubsequences(vector<int>& nums) {
set<vector<int>> res;
vector<int> one;
find(0,nums, one, res);
return vector<vector<int>>(res.begin(),res.end());
}
void find(int p, vector<int>& nums, vector<int>& one, set<vector<int>>& res) {
if(p==nums.size()) {
if(one.size()>1) res.insert(one);
return;
}
if(one.empty()||nums[p]>=one.back()) {
one.push_back(nums[p]);
find(p+1,nums,one,res);
one.pop_back();
}
find(p+1,nums,one,res);
}

方法三:

We can also generate all increasing subsequences by adding each number to the current sequencies iteratively and use set to remove duplicates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector<vector<int>> findSubsequences(vector<int>& nums) {
vector<vector<int>> seq(1);
set<vector<int>> bst;
for(int i=0;i<nums.size();i++) {
int n = seq.size();
for(int j=0;j<n;j++)
if(seq[j].empty() || seq[j].back()<=nums[i]) {
seq.push_back(seq[j]);
seq.back().push_back(nums[i]);
if(seq.back().size()>1) bst.insert(seq.back());
}
}
return vector<vector<int>>(bst.begin(),bst.end());
}

方法四:

We can do better by not generating duplicates. When adding a duplicate number to existing sequences, we don’t need to add to all sequences because that will create duplicate sequence. We only need to add to the sequences created since adding this number last time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vector<vector<int>> findSubsequences(vector<int>& nums) {
vector<vector<int>> res(1);
unordered_map<int,int> ht;
for(int i=0;i<nums.size();i++) {
int n = res.size();
int start = ht[nums[i]];
ht[nums[i]] = n;
for(int j=start;j<n;j++)
if(res[j].empty() || res[j].back()<=nums[i]) {
res.push_back(res[j]);
res.back().push_back(nums[i]);
}
}
for(int i=res.size()-1;i>=0;i--)
if(res[i].size()<2) {
swap(res[i],res.back());
res.pop_back();
}
return res;
}

方法五:

Duplicates can also be avoided in recursion. Starting from a given number, we pick the next number. We cache the numbers already tried to avoid duplicates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vector<vector<int>> findSubsequences(vector<int>& nums) {
vector<vector<int>> res;
vector<int> one;
find(0,nums,one,res);
return res;
}
void find(int p, vector<int>& nums, vector<int>& one, vector<vector<int>>& res) {
int n = nums.size();
if(one.size()>1) res.push_back(one);
unordered_set<int> ht;
for(int i=p;i<n;i++) {
if((!one.empty() && nums[i] < one.back()) || ht.count(nums[i])) continue;
ht.insert(nums[i]);
one.push_back(nums[i]);
find(i+1,nums,one,res);
one.pop_back();
}
}
谢谢你,可爱的朋友。