-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay71.cpp
More file actions
32 lines (31 loc) · 841 Bytes
/
Day71.cpp
File metadata and controls
32 lines (31 loc) · 841 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 <= 1){
return nums;
}
sort(nums.begin(), nums.end());
vector<int> dp(n, 1);
vector<int> temp(n, -1);
int maxi = 0;
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
if(nums[i] % nums[j] == 0 && dp[i] < dp[j] + 1){
dp[i] = dp[j] + 1;
temp[i] = j;
}
}
if(dp[i] > dp[maxi]){
maxi = i;
}
}
vector<int> ans;
while(maxi != -1){
ans.push_back(nums[maxi]);
maxi = temp[maxi];
}
sort(ans.begin(), ans.end());
return ans;
}
};