您的位置:首页 > 其它

[Leetcode]368. Largest Divisible Subset

2016-07-18 15:45 295 查看
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj)
of elements in this subset satisfies: Si % Sj =
0 or Sj % Si = 0.

If there are multiple solutions, return any subset is fine.

Example 1:
nums: [1,2,3]

Result: [1,2] (of course, [1,3] will also be ok)


Example 2:
nums: [1,2,4,8]

Result: [1,2,4,8]

class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
if (nums.empty()) {
return {};
}
sort(nums.begin(), nums.end());
vector<int> dp(nums.size() , 1);
vector<int> prev(nums.size(), -1);
int maxIdx = 0;
for (int i = 0; i < nums.size(); ++i) {
for (int j = 0; j < i; ++j) {
if (nums[i] % nums[j] == 0) {
if (dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
prev[i] = j;
}
}
}
if (dp[maxIdx] < dp[i]) {
maxIdx = i;
}
}

vector<int> result;
for (int i = maxIdx; i != -1; i = prev[i]) {
result.push_back(nums[i]);
}
return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode