-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-LargestDivisibleSubset.cpp
More file actions
32 lines (32 loc) · 952 Bytes
/
13-LargestDivisibleSubset.cpp
File metadata and controls
32 lines (32 loc) · 952 Bytes
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
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
int n = nums.size();
if (n == 0) return {};
vector<int> dp(n, 0);
vector<int> last(n, n);
sort(nums.begin(), nums.end());
dp[n-1] = 1;
int maxmGlobl = n-1;
for (int i = n - 2; i >= 0; i--) {
int maxm = i;
for (int j = i + 1; j < n; j++)
if (nums[j] % nums[i] == 0)
if (dp[j] > dp[maxm]) {
maxm = j;
last[i] = maxm;
}
dp[i] = 1 + dp[maxm];
if (dp[i] > dp[maxmGlobl]) {
maxmGlobl = i;
}
}
vector<int> ans;
while (last[maxmGlobl] != n) {
ans.push_back(nums[maxmGlobl]);
maxmGlobl = last[maxmGlobl];
}
ans.push_back(nums[maxmGlobl]);
return ans;
}
};