From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/non_decreasing_subseq.cpp | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 other/non_decreasing_subseq.cpp (limited to 'other/non_decreasing_subseq.cpp') diff --git a/other/non_decreasing_subseq.cpp b/other/non_decreasing_subseq.cpp new file mode 100644 index 0000000..b94510f --- /dev/null +++ b/other/non_decreasing_subseq.cpp @@ -0,0 +1,46 @@ +// 491. Non-decreasing Subsequences +#include "leetcode.h" + +/* + * given: integer array 'nums' + * return: all possible different non-decreasing subsequences + * of given array with at least two elements. return value + * can be in any order + */ + +class Solution { +public: + vector> findSubsequences(vector &nums) { + set> seqs = {vector(0)}; + for (int i = 0; i < nums.size(); i++) { + vector> built(seqs.size()); + copy(seqs.begin(), seqs.end(), built.begin()); + for (auto s : built) + if (s.empty() || nums[i] >= s.back()) { + s.push_back(nums[i]); + seqs.insert(s); + } + } + vector> ans; + for (auto s : seqs) + if (s.size() > 1) + ans.push_back(s); + return ans; + } +}; + +int main() { + Solution obj; + vector n1 = {4, 6, 7, 7}, n2 = {4, 4, 3, 2, 1}; + for (auto i : obj.findSubsequences(n1)) { + for (auto j : i) + printf("%d ", j); + printf("\n"); + } + printf("\n"); + for (auto i : obj.findSubsequences(n2)) { + for (auto j : i) + printf("%d ", j); + printf("\n"); + } +} -- cgit v1.2.3