-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY 19
More file actions
31 lines (27 loc) · 756 Bytes
/
DAY 19
File metadata and controls
31 lines (27 loc) · 756 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
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
int n=nums.size(), maxi=1, num=-1;
vector<int>v;
sort(nums.begin(), nums.end());
vector<int>dp(n, 1);
for(int i=1; i<n; i++){
for(int j=0; j<i; j++){
if(!(nums[i]%nums[j]) && dp[i]<dp[j]+1){
dp[i]=dp[j]+1;
if(maxi<dp[i]){
maxi=dp[i];
}
}
}
}
for(int i=n-1; i>=0; i--){
if(maxi==dp[i] && (num==-1 || !(num%nums[i]))){
v.push_back(nums[i]);
maxi--;
num=nums[i];
}
}
return v;
}
};