generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
35 lines (29 loc) · 803 Bytes
/
solution.cpp
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
#include <algorithm>
#include <vector>
class Solution {
public:
std::vector<int> largestDivisibleSubset(std::vector<int>& nums) {
const int n = nums.size();
std::sort(nums.begin(), nums.end());
std::vector<int> prevOf(n, -1);
std::vector<int> countOf(n, 1);
int candidate = 0;
for (int i = 0; i < n; ++i) {
for (int j = i - 1; j >= 0; --j) {
if (nums[i] % nums[j] == 0 && countOf[j] + 1 > countOf[i]) {
prevOf[i] = j;
countOf[i] = countOf[j] + 1;
}
}
if (countOf[i] > countOf[candidate]) {
candidate = i;
}
}
std::vector<int> output{};
output.reserve(countOf[candidate]);
for (int i = candidate; i >= 0; i = prevOf[i]) {
output.push_back(nums[i]);
}
return output;
}
};