Increasing Subsequences – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Given an integer array nums
, return all the different possible increasing subsequences of the given array with at least two elements. You may return the answer in any order.
The given array may contain duplicates, and two equal integers should also be considered a special case of increasing sequence.
Example 1:
Input: nums = [4,6,7,7] Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
Example 2:
Input: nums = [4,4,3,2,1] Output: [[4,4]]
Constraints:
1 <= nums.length <= 15
-100 <= nums[i] <= 100
C++ Increasing Subsequences LeetCode Solution
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]);
}
}
}
};
Java Increasing Subsequences LeetCode Solution
public class Solution {
public List<List<Integer>> findSubsequences(int[] nums) {
List<List<Integer>> res = new LinkedList<>();
helper(new LinkedList<Integer>(), 0, nums, res);
return res;
}
private void helper(LinkedList<Integer> list, int index, int[] nums, List<List<Integer>> res){
if(list.size()>1) res.add(new LinkedList<Integer>(list));
Set<Integer> used = new HashSet<>();
for(int i = index; i<nums.length; i++){
if(used.contains(nums[i])) continue;
if(list.size()==0 || nums[i]>=list.peekLast()){
used.add(nums[i]);
list.add(nums[i]);
helper(list, i+1, nums, res);
list.remove(list.size()-1);
}
}
}
}
Python 3 Increasing Subsequences LeetCode Solution
def findSubsequences(self, nums):
subs = {()}
for num in nums:
subs |= {sub + (num,)
for sub in subs
if not sub or sub[-1] <= num}
return [sub for sub in subs if len(sub) >= 2]
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1