您的位置:首页 > 其它

leetcode.368. Largest Divisible Subset

2016-07-10 16:59 134 查看
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) {
int i,j,len=nums.size(),m=0,mi;
vector<int> T(len,0);
vector<int> son(len,0);
sort(nums.begin(),nums.end());
for(i=0;i<len;i++){
for(j=i;j>=0;j--)
{
if(nums[i]%nums[j]==0&&T[j]+1>T[i]){
T[i]=T[j]+1;
son[i]=j;
}
}
if(T[i]>m){
m=T[i];
mi=i;
}
}
vector<int> re;
for(i=0;i<m;i++){
re.insert(re.begin(),nums[mi]);
mi=son[mi];
}
return re;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: