LandscapeMi

landscapemi的博客

leetcode:数学: Largest Divisible Subset(368)

https://leetcode.com/problems/largest-divisible-subset/


http://blog.csdn.net/qq508618087/article/details/51767785


class Solution {  
public:  
    vector<int> largestDivisibleSubset(vector<int>& nums) {  
        if(nums.size() ==0) return {};   
        sort(nums.begin(), nums.end(), greater<int>());  
        int len = nums.size(), curMax=1, k=0;  
        vector<int> par(len), dp(len, 1), result;  
        for(int i =0; i < len; i++) par[i] = i;  
        for(int i =1; i < len; i++)  
        {  
            for(int j =0; j < i; j++)  
            {  
                if(nums[j]%nums[i]!=0) continue;  
                if(dp[i] < dp[j]+1) par[i] = j, dp[i]=dp[j]+1;  
                if(dp[i] > curMax) curMax = dp[i], k = i;  
            }  
        }  
        while(par[k] != k)  
        {  
            result.push_back(nums[k]);  
            k = par[k];  
        }  
        result.push_back(nums[k]);  
        return result;  
    }  
};  
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mijian1207mijian/article/details/52384666
个人分类: leetcode(c++)
上一篇leetcode:数学:Water and Jug Problem(365)
下一篇leetcode:数学:spuer pow(372)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭