Skip to content

Commit 4c98a35

Browse files
committed
0491 solved.
1 parent 2d4be06 commit 4c98a35

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
project(cpp_0491)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_0491 main.cpp)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// Source : https://leetcode.com/problems/non-decreasing-subsequences/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-01-19
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <set>
8+
9+
using namespace std;
10+
11+
12+
/// Backtrack
13+
/// Time Compelxity: O(2^n)
14+
/// Space Compelxity: O(n)
15+
class Solution {
16+
public:
17+
vector<vector<int>> findSubsequences(vector<int>& nums) {
18+
19+
int n = nums.size();
20+
set<vector<int>> res;
21+
vector<int> cur;
22+
dfs(n, nums, 0, cur, res);
23+
return vector<vector<int>>(res.begin(), res.end());
24+
}
25+
26+
private:
27+
void dfs(int n, const vector<int>& nums, int start,
28+
vector<int>& cur, set<vector<int>>& res){
29+
30+
if(start == n){
31+
if(cur.size() >= 2) res.insert(cur);
32+
return;
33+
}
34+
35+
dfs(n, nums, start + 1, cur, res);
36+
37+
if(cur.empty() || nums[start] >= cur.back()){
38+
cur.push_back(nums[start]);
39+
dfs(n, nums, start + 1, cur, res);
40+
cur.pop_back();
41+
}
42+
}
43+
};
44+
45+
46+
int main() {
47+
48+
return 0;
49+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
522522
| 488 | [Zuma Game](https://leetcode.com/problems/zuma-game/) | [] | [C++](0001-0500/0488-Zuma-Game/cpp-0488/) | | |
523523
| 489 | [Robot Room Cleaner](https://leetcode.com/problems/robot-room-cleaner/) | [solution](https://leetcode.com/problems/robot-room-cleaner/solution/) | [C++](0001-0500/0489-Robot-Room-Cleaner/cpp-0489/) | | |
524524
| 490 | [The Maze](https://leetcode.com/problems/the-maze/description/) | [solution](https://leetcode.com/problems/the-maze/solution/) | [C++](0001-0500/0490-The-Maze/cpp-0490/) | | |
525-
| | | | | | |
525+
| 491 | [Non-decreasing Subsequences](https://leetcode.com/problems/non-decreasing-subsequences/description/) | [] | [C++](0001-0500/0491-Non-decreasing-Subsequences/cpp-0491/) | | |
526526
| 492 | [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle/) | [] | [C++](0001-0500/0492-Construct-the-Rectangle/cpp-0492/) | | |
527527
| | | | | | |
528528
| 494 | [Target Sum](https://leetcode.com/problems/target-sum/description/) | [solution](https://leetcode.com/problems/target-sum/solution/) | [C++](0001-0500/0494-Target-Sum/cpp-0494/) | | |

0 commit comments

Comments
 (0)