leetcode:数学: Largest Divisible Subset(368)

原创 2016年08月31日 14:21:32

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;  
    }  
};  
版权声明:本文为博主原创文章,未经博主允许不得转载。

[leetcode] 368. Largest Divisible Subset 解题报告

题目链接: https://leetcode.com/problems/largest-divisible-subset/ Given a set of distinct positive int...
  • qq508618087
  • qq508618087
  • 2016年06月27日 12:30
  • 7378

【Leetcode】Largest Divisible Subset

题目链接:https://leetcode.com/problems/largest-divisible-subset/题目: Given a set of distinct positive in...
  • yeqiuzs
  • yeqiuzs
  • 2016年06月28日 10:18
  • 3590

Leetcode 368. Largest Divisible Subset

368. Largest Divisible Subset Total Accepted: 939 Total Submissions: 2935 Difficulty: Medium ...
  • fantasiasango
  • fantasiasango
  • 2016年06月29日 03:16
  • 1145

368. Largest Divisible Subset(重点)

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) o...
  • gao1440156051
  • gao1440156051
  • 2016年07月26日 21:39
  • 643

Largest Divisible Subset

题目链接: https://leetcode.com/problems/largest-divisible-subset/ Given a set of distinct positiv...
  • jeason29
  • jeason29
  • 2016年06月27日 13:52
  • 623

LeetCode - 368 - Largest Divisible Subset

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of...
  • j6635788
  • j6635788
  • 2017年08月02日 17:59
  • 62

LeetCode[368] Largest Divisible Subset

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
  • wrsophia_977
  • wrsophia_977
  • 2016年07月23日 19:48
  • 80

LeetCode 368: Largest Divisible Subset

368. Largest Divisible SubsetDifficulty: Medium Given a set of distinct positive integers, find the...
  • Cycle1413
  • Cycle1413
  • 2016年07月15日 14:46
  • 195

leetcode(368):Largest Divisible Subset

原题: Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj...
  • sunyangwei1993
  • sunyangwei1993
  • 2016年08月10日 21:49
  • 310

LeetCode #368: Largest Divisible Subset

Problem Statement(Source) Given a set of distinct positive integers, find the largest subset such th...
  • junchen1992
  • junchen1992
  • 2016年09月18日 22:49
  • 127
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:数学: Largest Divisible Subset(368)
举报原因:
原因补充:

(最多只允许输入30个字)