-
Notifications
You must be signed in to change notification settings - Fork 9
/
combinations_subsets.rs
227 lines (207 loc) · 5.81 KB
/
combinations_subsets.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/** https://leetcode.com/problems/subsets
# subsets-i: 输入数组无重复项(简单版)
Input: vec![1, 2, 3]
Output: vec_vec![[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
## 三种解决思路
1. 组合数: 将问题的规模缩小为从C(n,0)+C(n,1),也就是从3个选0个,1个,2个,3个集合的和
2. BFS迭代: curr=curr.append(curr.each+nums[i])
3. DFS决策: 遍历1,2,3时每个都有选或不选的决策
## C(n,k)解法: 如何缩小问题的规模?数学公式?
将返回值的二维数组按长度分组,不难发现以下规律
C(n,0): [ ]
C(n,1): [1] [2] [3]
C(n,2): [1,2] [1,3] [2,3]
C(n,3): [1,2,3]
所以可以将问题简化为编写一个 穷举从长度为n的数组中取k个的函数的不同组合
## subsets可能的搜索树?
注: 尖括号表示剪枝(不要的重复项)
```text
[ ]
[1] [2] [3]
[1,2] [1,3] <2,1> [2,3] <3,1> <3,2>
```
*/
fn subsets_combine(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut ret = vec![];
for k in 0..=nums.len() {
combine_n_k(&nums, k, &mut ret);
}
ret
}
/// https://leetcode.com/problems/combinations/
/// itertools.combinations(nums, k)
/// 非递归的combine算法用到了二进制的字典序,过于难
fn combine_n_k(nums: &[i32], k: usize, ret: &mut Vec<Vec<i32>>) {
/// i: unused/un_choose num index in nums
fn helper(
unused_start: usize,
cur: &mut Vec<i32>,
ret: &mut Vec<Vec<i32>>,
nums: &[i32],
k: usize,
) {
if cur.len() == k {
ret.push(cur.clone());
return;
}
for unused_idx in unused_start..nums.len() {
cur.push(nums[unused_idx]);
helper(unused_idx + 1, cur, ret, nums, k);
cur.pop().unwrap();
}
}
let mut cur = Vec::with_capacity(k);
helper(0, &mut cur, ret, nums, k);
}
/// https://leetcode.com/problems/combinations/
fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
let mut ret = vec![];
let nums: Vec<i32> = (1..).take(n as usize).collect();
combine_n_k(&nums, k as usize, &mut ret);
ret
}
/**
```python
output = [[]]
for num in nums:
output += [curr + [num] for curr in output]
return output
```
## ⭐subsets BFS搜索树
```text
[]
[] [1]
[] [1] [2] [1,2]
[] [1] [2] [1,2] [3] [1,3] [2,3] [1,2,3]
```
check DFS search tree on problem sum-of-all-subset-xor-totals
数据结构上用不断遍历上一次队列长度进行出队处理后再入队,或者用sentinel_node的队列也可以
但是在内存利用率上远不如两个新旧数组间互相迭代(例如二叉树的层级遍历)
根据每个num的选或不选组成二叉树
*/
fn subsets_bfs(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut last: Vec<Vec<i32>> = Vec::with_capacity(2_usize.pow(nums.len() as u32));
last.push(Vec::with_capacity(0));
for num in nums {
let mut curr = last.clone();
for each_curr in &mut curr {
each_curr.push(num);
}
last.append(&mut curr);
}
last.sort_by_cached_key(Vec::len);
last
}
struct SubsetsDfs {
nums: Vec<i32>,
len: usize,
cur: Vec<i32>,
ret: Vec<Vec<i32>>,
}
/**
## ⭐subsets DFS搜索树
left_child : not select
right_child: select
```text
[]
1: [] [1]
2: [] [2] [1] [2]
```
*/
impl SubsetsDfs {
fn dfs(&mut self, index: usize) {
if index == self.len {
self.ret.push(self.cur.clone());
return;
}
// select current num
self.cur.push(self.nums[index]);
self.dfs(index + 1);
self.cur.pop();
// doesn't select current num
self.dfs(index + 1);
}
}
fn subsets_dfs(nums: Vec<i32>) -> Vec<Vec<i32>> {
let len = nums.len();
let mut helper = SubsetsDfs {
nums,
len,
cur: Vec::new(),
ret: Vec::new(),
};
helper.dfs(0);
helper.ret.sort_by_cached_key(Vec::len);
helper.ret
}
#[test]
fn test_subsets() {
let test_cases = vec![(
vec![1, 2, 3],
vec_vec![[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]],
)];
for (input, output) in test_cases {
assert_eq!(subsets_combine(input.clone()), output);
assert_eq!(subsets_bfs(input.clone()), output);
assert_eq!(subsets_dfs(input), output);
}
}
/// https://leetcode.com/problems/sum-of-all-subset-xor-totals/
fn subsets_xor_sum(nums: Vec<i32>) -> i32 {
let mut last_subsets = vec![vec![]];
for num in nums {
let mut curr_subsets = last_subsets.clone();
for each_subset in &mut curr_subsets {
each_subset.push(num);
}
last_subsets.append(&mut curr_subsets);
}
last_subsets
.into_iter()
.map(|subset| subset.into_iter().fold(0, |a, b| a ^ b))
.sum()
}
/**
## DFS搜索树
left_child : not select current num
right_child: select current num
```text
[]
1: [] [1]
```
*/
fn subsets_xor_sum_dfs(nums: Vec<i32>) -> i32 {
struct Dfs {
nums: Vec<i32>,
len: usize,
total_xor_sum: i32,
}
impl Dfs {
fn dfs(&mut self, index: usize, curr_xor_sum: i32) {
if index == self.len {
self.total_xor_sum += curr_xor_sum;
return;
}
// select current num
self.dfs(index + 1, curr_xor_sum ^ self.nums[index]);
// doesn't select current num
self.dfs(index + 1, curr_xor_sum);
}
}
let len = nums.len();
let mut helper = Dfs {
nums,
len,
total_xor_sum: 0,
};
helper.dfs(0, 0);
helper.total_xor_sum
}
#[test]
fn test_subsets_xor_sum() {
const TEST_CASES: [(&[i32], i32); 1] = [(&[5, 1, 6], 28)];
for (nums, output) in TEST_CASES {
assert_eq!(subsets_xor_sum(nums.to_owned()), output);
assert_eq!(subsets_xor_sum_dfs(nums.to_owned()), output);
}
}