LeetCode491 非递减子序列

前言

题目: 491. 非递减子序列
文档: 代码随想录------非递减子序列
编程语言: C++
解题状态: 好难...

思路

注意不能对原数组进行排序。

代码

cpp 复制代码
class Solution {
private:
    vector<vector<int>> res;
    vector<int> path;
    void backtracking(vector<int>& nums, int startIndex) {
        if (path.size() > 1) {
            res.push_back(path);
        }

        unordered_set<int> uset;

        for (int i = startIndex; i < nums.size(); i++) {
            if ((!path.empty() && nums[i] < path.back()) || uset.find(nums[i]) != uset.end()) {
                continue;
            }
            uset.insert(nums[i]);
            path.push_back(nums[i]);
            backtracking(nums, i + 1);
            path.pop_back();
        }
    }
public:
    vector<vector<int>> findSubsequences(vector<int>& nums) {
        res.clear();
        path.clear();
        backtracking(nums, 0);
        return res;
    }
};
  • 时间复杂度: O ( n ∗ 2 n ) O(n*2^n) O(n∗2n)
  • 空间复杂度: O ( n ) O(n) O(n)
相关推荐
白杨尚青41 分钟前
C++进阶篇(六):map 和 set 的模拟实现——用红黑树封装 mymap/myset(迭代器 + operator[])
开发语言·c++·迭代器·stl·set·map·红黑树
好悬给我拽开线3 小时前
ZILOT 论文解析
算法
kloc3 小时前
聊天室项目入口网关子服务的实现
linux·c++
饮水桶_T4 小时前
JAVA IO流笔记
开发语言·数据结构·算法
星夜离尘565 小时前
MATLAB 离散数据分箱:半开区间、末端点与计数、加权和、平均
算法·matlab
Pniubi5 小时前
力扣76.最小覆盖子串
算法·leetcode·职场和发展
地平线开发者6 小时前
征程6|用 hrt\_model\_exec 看清 YOLOv5x 的输入、原始输出和后处理
人工智能·算法
csdn_aspnet7 小时前
C++ 3个有序点的方向(Orientation of 3 ordered points)
c++·算法
赤生云电竞7 小时前
GPU算力共享平台架构实践:任务分发与资源隔离的关键配置
算法·架构·gpu算力
小七在进步8 小时前
string的使用(2)
c++