-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0368_Largest_Divisible_Subset.cpp
More file actions
36 lines (34 loc) · 1.18 KB
/
0368_Largest_Divisible_Subset.cpp
File metadata and controls
36 lines (34 loc) · 1.18 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
35
36
#pragma GCC optimize("Ofast","inline","ffast-math","unroll-loops","no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native","f16c")
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n = nums.size();
vector<int>dp(n , 1) , hash(n);
int maxi = 1;
int lastindex = 0;
sort(nums.begin() , nums.end());
for(int ind = 0 ; ind < n ; ind++ ){
hash[ind] = ind;
for(int prev = 0 ; prev < ind ; prev++){
if(nums[ind] % nums[prev] == 0 && 1 + dp[prev] > dp[ind]){
dp[ind] = 1 + dp[prev];
hash[ind] = prev;
}
}
if(maxi < dp[ind]){
maxi = dp[ind];
lastindex = ind;
}
}
vector<int>temp;
temp.push_back(nums[lastindex]);
while(hash[lastindex] != lastindex){
lastindex = hash[lastindex];
temp.push_back(nums[lastindex]);
}
reverse(temp.begin() , temp.end());
return temp;
}
};