Skip to content

Commit 6cc6a2c

Browse files
committed
leetcode
1 parent f2fc981 commit 6cc6a2c

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
3+
4+
-* 491. Non-decreasing Subsequences *-
5+
6+
Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.
7+
8+
9+
10+
Example 1:
11+
12+
Input: nums = [4,6,7,7]
13+
Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
14+
Example 2:
15+
16+
Input: nums = [4,4,3,2,1]
17+
Output: [[4,4]]
18+
19+
20+
Constraints:
21+
22+
1 <= nums.length <= 15
23+
-100 <= nums[i] <= 100
24+
25+
*/
26+
27+
import 'dart:collection';
28+
29+
class A {
30+
List<List<int>> findSubsequences(List<int> nums) {
31+
List<List<int>> ans = [];
32+
// use a List to store the unique sub sequences
33+
dfs(nums, 0, <int>[], ans);
34+
return ans;
35+
}
36+
37+
void dfs(List<int> nums, int s, List<int> path, List<List<int>> ans) {
38+
if (path.length > 1) ans.add(path.toList());
39+
40+
HashSet<int> used = HashSet();
41+
42+
for (int i = s; i < nums.length; ++i) {
43+
if (used.contains(nums[i])) continue;
44+
// if the path is empty or the current element is greater than or equal to the last element in the path
45+
if (path.isEmpty || nums[i] >= path.last) {
46+
// add the current element to the used and recursively call dfs
47+
used.add(nums[i]);
48+
path.add(nums[i]);
49+
// recursively call dfs without adding the current element to the path
50+
dfs(nums, i + 1, path, ans);
51+
path.removeLast(); // remove the last element from the path before returning
52+
}
53+
}
54+
}
55+
}
56+
57+
class B {
58+
late List<List<int>> res;
59+
List<List<int>> findSubsequences(List<int> nums) {
60+
res = [];
61+
helper(<int>[], nums, 0);
62+
return res;
63+
}
64+
65+
void helper(List<int> list, List<int> nums, int start) {
66+
HashSet<int> set = HashSet();
67+
if (start == nums.length) {
68+
return;
69+
}
70+
for (int i = start; i < nums.length; i++) {
71+
if (set.contains(nums[i])) {
72+
continue;
73+
}
74+
set.add(nums[i]);
75+
if (list.length == 0 || nums[i] >= list[list.length - 1]) {
76+
list.add(nums[i]);
77+
if (list.length > 1) {
78+
res.add(list.toList());
79+
}
80+
helper(list, nums, i + 1);
81+
list.remove(list.length - 1);
82+
}
83+
}
84+
}
85+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
func findSubsequences(nums []int) (res [][]int) {
4+
var dfs func(int, []int)
5+
dfs = func(l int, sl []int) {
6+
if len(sl) > 1 {
7+
cp := make([]int, len(sl))
8+
copy(cp, sl)
9+
res = append(res, cp)
10+
}
11+
seen := make(map[int]bool, len(nums)-l)
12+
for r := l; r < len(nums); r++ {
13+
if (l > 0 && nums[r] < nums[l-1]) || seen[nums[r]] {
14+
continue
15+
}
16+
seen[nums[r]] = true
17+
dfs(r+1, append(sl, nums[r]))
18+
}
19+
}
20+
21+
dfs(0, []int{})
22+
23+
return res
24+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 🔥 100% Fast 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 Depth first Search
4+
5+
### Approach
6+
7+
Backtracking using HashSet
8+
9+
### Complexity
10+
11+
#### - Time complexity : O(n.2^n)
12+
13+
#### - Space complexity : O(n^2)
14+
15+
```dart
16+
class Solution {
17+
List<List<int>> findSubsequences(List<int> nums) {
18+
List<List<int>> ans = [];
19+
// use a List to store the unique sub sequences
20+
dfs(nums, 0, <int>[], ans);
21+
return ans;
22+
}
23+
24+
void dfs(List<int> nums, int s, List<int> path, List<List<int>> ans) {
25+
if (path.length > 1) ans.add(path.toList());
26+
27+
HashSet<int> used = HashSet();
28+
29+
for (int i = s; i < nums.length; ++i) {
30+
if (used.contains(nums[i])) continue;
31+
// if the path is empty or the current element is greater than or equal to the last element in the path
32+
if (path.isEmpty || nums[i] >= path.last) {
33+
// add the current element to the used and recursively call dfs
34+
used.add(nums[i]);
35+
path.add(nums[i]);
36+
// recursively call dfs without adding the current element to the path
37+
dfs(nums, i + 1, path, ans);
38+
path.removeLast(); // remove the last element from the path before returning
39+
}
40+
}
41+
}
42+
}
43+
```

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
189189
- [**926.** Flip String to Monotone Increasing](FlipStringToMonotoneIncreasing/flip_string_to_monotone_increasing.dart)
190190
- [**918.** Maximum Sum Circular Sub-Array](MaximumSumCircularSubarray/maximum_sum_circular_subarray.dart)
191191
- [**974.** SubArray Sums Divisible by K](SubarraySumsDivisibleByK/subarray_sums_divisible_by_k.dart)
192+
- [**491.** Non-decreasing Subsequences](Non-DecreasingSubsequences/non_decreasing_subsequences.dart)
192193

193194
## Reach me via
194195

0 commit comments

Comments
 (0)