Skip to content

Commit

Permalink
solve feb09 problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabil-Salah committed Feb 9, 2024
1 parent 0adb668 commit a0d9d74
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
int n=(int)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;
}
};

// Time Complexity- O(n^2)
//Space Complexity-o(n)

0 comments on commit a0d9d74

Please sign in to comment.