-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path77.combinations.cc
More file actions
48 lines (41 loc) · 1.07 KB
/
77.combinations.cc
File metadata and controls
48 lines (41 loc) · 1.07 KB
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
#include <cstdio>
#include <vector>
#include "leetcode_common_struct.h"
// Runtime 46ms, beats 51.94%
// Memory 19MB, beats 25.2%
std::vector<std::vector<int>> combine(int n, int k) {
std::vector<std::vector<std::vector<int>>> dp_x;
dp_x.reserve(k);
dp_x.push_back(std::vector<std::vector<int>>());
auto &dp_last = dp_x.back();
for (auto i = 1; i < n + 1; ++i) {
dp_last.push_back({i});
}
for (auto i = 1; i < k; ++i) {
const auto &dp = dp_x.back();
dp_x.push_back(std::vector<std::vector<int>>());
auto &new_dp = dp_x.back();
auto last = n - k + 1 + i;
for (auto dpi : dp) {
auto copy_vec = dpi;
copy_vec.push_back(0);
for (auto j = dpi.back() + 1; j <= last; ++j) {
copy_vec.back() = j;
new_dp.push_back(copy_vec);
}
}
}
return dp_x.back();
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "program <n> <k>");
return -1;
}
int n = std::atoi(argv[1]);
int k = std::atoi(argv[2]);
auto combs = combine(n, k);
for (const auto &vec : combs) {
PrintVect(vec);
}
}