491. Increasing Subsequences**

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:

  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.
public class Solution {
    public List<List<Integer>> findSubsequences(int[] nums) {
        Set<List<Integer>> list = new HashSet<List<Integer>>();
        backtrack(list,new ArrayList<>(),nums,0);
        return new ArrayList(list);
    }
    private void backtrack(Set<List<Integer>> list, List<Integer> tempList, int[] nums,int start){
        if (tempList.size()>1) list.add(new ArrayList<>(tempList));
        for(int i=start;i<nums.length;i++){
            if(tempList.size()==0||tempList.get(tempList.size()-1)<=nums[i]){
               tempList.add(nums[i]);
               backtrack(list,tempList,nums,i+1);
               tempList.remove(tempList.size()-1);
            }
        }
    }
}

总结:万变不离其宗,但是几天没看就生疏了。


### 基于 LCS 算法的衍生算法及相关算法 #### 1. **最长上升子序列 (Longest Increasing Subsequence, LIS)** LIS 是一种特殊的 LCS 变体,用于寻找数组中的最长单调递增子序列。它可以通过动态规划解决,时间复杂度为 \(O(n^2)\),而通过二分查找优化后可达到 \(O(n \log n)\)[^3]。 以下是基于二分查找的 LIS 实现: ```python def lis(nums): tails = [] for num in nums: left, right = 0, len(tails) while left < right: mid = (left + right) // 2 if tails[mid] < num: left = mid + 1 else: right = mid if left == len(tails): tails.append(num) else: tails[left] = num return len(tails) ``` --- #### 2. **编辑距离 (Edit Distance)** 虽然编辑距离的核心目标是计算两个字符串之间的最小操作次数(插入、删除或替换),但它与 LCS 密切相关。事实上,编辑距离可以视为 LCS 的扩展版本,因为它不仅关注共同部分,还考虑如何使两串完全一致[^4]。 其状态转移方程如下: \[ D(i, j) = \begin{cases} j & \text{if } i = 0 \\ i & \text{if } j = 0 \\ D(i-1, j-1) & \text{if } X[i] = Y[j] \\ \min(D(i-1,j), D(i,j-1), D(i-1,j-1)) + 1 & \text{otherwise} \end{cases} \] --- #### 3. **最短公共超序列 (Shortest Common Supersequence, SCS)** SCS 是指找到一个最短的字符串,使得给定的两个字符串都作为它的子序列。此问题可以直接利用 LCS 来解决——即先找出两个字符串的 LCS,再将其余字符按顺序拼接至结果中[^1]。 假设 `lcs_length` 表示 LCS 长度,则 SCS 长度为: \[ |X| + |Y| - \text{lcs\_length}(X,Y) \] --- #### 4. **最大重复子序列 (Maximal Repeated Subsequences)** 该问题是寻找在一个字符串内部多次出现的最大长度子序列。其实质是对同一个字符串应用两次 LCS 计算,从而发现重叠的部分[^2]。 --- #### 5. **多序列比对 (Multiple Sequence Alignment)** 这是生物信息学领域的重要问题之一,涉及多个 DNA 或蛋白质序列的同时比较。尽管传统 LCS 方法仅适用于成对序列对比,但其思想被广泛应用于更复杂的多序列比对场景中。 --- ### 总结 上述算法均以 LCS 为核心思想进行了不同程度的延伸和发展。无论是单序列分析还是跨序列匹配,这些方法都在实际应用场景中发挥了重要作用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值