-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathLargest_Divisible_Subset.cpp
More file actions
31 lines (31 loc) · 944 Bytes
/
Largest_Divisible_Subset.cpp
File metadata and controls
31 lines (31 loc) · 944 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
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
vector<int> result;
if(nums.empty()) return result;
int n = (int)nums.size();
sort(nums.begin(), nums.end());
vector<int> dp(n, 1);
int indx = 0, maxLength = dp[0];
for(int i = 1; i < n; ++i) {
for(int j = 0; j < i; ++j) {
if(nums[i] % nums[j] == 0) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
if(dp[i] > maxLength) {
maxLength = dp[i];
indx = i;
}
}
result.resize(maxLength);
result[--maxLength] = nums[indx];
for(int i = indx - 1; i >= 0; --i) {
if(dp[i] + 1 == dp[indx] and nums[indx] % nums[i] == 0) {
result[--maxLength] = nums[i];
indx = i;
}
}
return result;
}
};