website/content.en/ChapterFour/0400~0499/0491.Non-decreasing-Subsequences.md
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.
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:
Given an integer array, your task is to find all increasing subsequences of the array, and the length of an increasing subsequence should be at least 2.
Notes:
package leetcode
func findSubsequences(nums []int) [][]int {
c, visited, res := []int{}, map[int]bool{}, [][]int{}
for i := 0; i < len(nums)-1; i++ {
if _, ok := visited[nums[i]]; ok {
continue
} else {
visited[nums[i]] = true
generateIncSubsets(nums, i, c, &res)
}
}
return res
}
func generateIncSubsets(nums []int, current int, c []int, res *[][]int) {
c = append(c, nums[current])
if len(c) >= 2 {
b := make([]int, len(c))
copy(b, c)
*res = append(*res, b)
}
visited := map[int]bool{}
for i := current + 1; i < len(nums); i++ {
if nums[current] <= nums[i] {
if _, ok := visited[nums[i]]; ok {
continue
} else {
visited[nums[i]] = true
generateIncSubsets(nums, i, c, res)
}
}
}
c = c[:len(c)-1]
return
}