-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path368. Largest Divisible Subset.cpp
42 lines (35 loc) · 1.12 KB
/
368. Largest Divisible Subset.cpp
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
37
38
39
40
41
42
// -----Approach: Tabulation ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/largest-divisible-subset/
Time: 56 ms (Beats 66.96%), Space: 9.1 MB (Beats 21.34%)
*/
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
int n= nums.size();
int mx= 1, lastIdx= 0;
sort(nums.begin(), nums.end());
vector<int> dp(n, 1), hash(n);
for(int idx=0; idx<n; idx++){
hash[idx]= idx;
for(int prevIdx=0; prevIdx<idx; prevIdx++){
if(nums[idx] % nums[prevIdx] == 0 && dp[idx] < dp[prevIdx] + 1){
dp[idx]= dp[prevIdx] + 1;
hash[idx]= prevIdx;
}
}
if(dp[idx] > mx){
mx= dp[idx];
lastIdx= idx;
}
}
vector<int> v;
v.push_back(nums[lastIdx]);
while(hash[lastIdx] != lastIdx){
lastIdx= hash[lastIdx];
v.push_back(nums[lastIdx]);
}
reverse(v.begin(), v.end());
return v;
}
};