-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestDivisibleSubset.cpp
More file actions
34 lines (33 loc) · 1.03 KB
/
largestDivisibleSubset.cpp
File metadata and controls
34 lines (33 loc) · 1.03 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
// Source: https://leetcode.com/problems/largest-divisible-subset/
// Author: Miao Zhang
// Date: 2021-02-04
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
if (nums.empty()) return {};
sort(nums.begin(), nums.end());
vector<int> dp(nums.size(), 0);
vector<int> parent(nums.size(), 0);
int maxval = 0;
int index = 0;
for (int i = 0; i < nums.size(); i++) {
for (int j = i - 1; j >= 0; j--) {
if ((nums[i] % nums[j] == 0) && (dp[i] < dp[j] + 1)) {
dp[i] = dp[j] + 1;
parent[i] = j;
if (dp[i] > maxval) {
maxval = dp[i];
index = i;
}
}
}
}
vector<int> res;
for (int j = 0; j <= maxval; j++) {
res.push_back(nums[index]);
index = parent[index];
}
reverse(res.begin(), res.end());
return res;
}
};