Skip to content

Create 15_largestDIvisibleSubset.cpp #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 11 November Leetcode Challenge 2021/15_largestDIvisibleSubset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
sort(begin(nums), end(nums));
int n = size(nums), max_i = 0;
// initially dp[i]=1 since we can always form subset of size=1 ending at i.
// pred[i]=-1 because we havent found any predecessors for any subset yet
vector<int> dp(n, 1), pred(n, -1), ans;
for(int i = 1; i < n; i++) {
for(int j = 0; j < i; j++)
// nums[i] should divide nums[j] if it is to be included in its subset (i.e dp[j])
// only include nums[i] in subset ending at j if resultant subset size (dp[j]+1) is larger than already possible (dp[i])
if(nums[i] % nums[j] == 0 && dp[i] < dp[j]+1)
dp[i] = dp[j]+1, pred[i] = j; // jth element will be predecessor to subset ending at ith element
if(dp[i] > dp[max_i]) max_i = i; // keep track of index where largest subset ends
}
// start with index where largest subset ended. Reconstruct from that point to the start
for(; max_i >= 0; max_i = pred[max_i])
ans.push_back(nums[max_i]);
return ans;
}
};