forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
largest-divisible-subset.cpp
38 lines (35 loc) · 1.06 KB
/
largest-divisible-subset.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
36
37
38
// Time: O(n^2)
// Space: O(n)
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
if (nums.empty()) {
return {};
}
sort(nums.begin(), nums.end());
// dp[i]: the size of the largest distinct subset of
// the first i+1 numbers including nums[i]
vector<int> dp(nums.size() , 1);
vector<int> prev(nums.size(), -1);
int largest_idx = 0;
for (int i = 0; i < nums.size(); ++i) {
for (int j = 0; j < i; ++j) {
if (nums[i] % nums[j] == 0) {
if (dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
prev[i] = j;
}
}
}
if (dp[largest_idx] < dp[i]) {
largest_idx = i;
}
}
vector<int> result;
for (int i = largest_idx; i != -1; i = prev[i]) {
result.emplace_back(nums[i]);
}
reverse(result.begin(), result.end());
return result;
}
};