368.[LeetCode] Largest Divisible Subset

条件:动态规划

当nums[j]%nums[i]==0时,dp[i] = max(dp[i], dp[j]+1)

为了返回数组,使用pair记录路径

class Solution {
public:
    vector<int> largestDivisibleSubset(vector<int>& nums) {     //变量和初始值的定义
        vector<int> res;
        if(nums.size()<1) return res;//临界条件
        sort(nums.begin(),nums.end(),greater<int>());//greater<int>()表示由大到小排序
        int len=nums.size(),k=0,curMax=1;//k代表取得curMax位置时候的位置。
        vector<int> dp(len,1),pair(len,0);
        for(int i=0;i<len;i++) pair[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) pair[i]=j,dp[i]=dp[j]+1;//pair[i]=j表示i的前一个是j//这个会把最大值覆盖掉吗?应该不会的,如果更新了dp[i],dp[i]是一直变大。
                if(curMax<dp[i]) k=i,curMax=dp[i];//更新curMax,并且更新k值
            }
        }

        while(k!=pair[k]){
            res.push_back(nums[k]),k=pair[k];
        }
        res.push_back(nums[k]);
        return res;
    }
};

  

原文地址:https://www.cnblogs.com/bright-mark/p/9537269.html

时间: 2024-10-28 10:46:09

368.[LeetCode] Largest Divisible Subset的相关文章

[LeetCode] Largest Divisible Subset 最大可整除的子集合

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] Re

Leetcode: Largest Divisible Subset

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] Re

368. Largest Divisible Subset

/* * 368. Largest Divisible Subset * 2016-7-16 by Mingyang * 和LIS很相似,dp[i]表示nums数组从0到i的最大集合的size. * 这题应该分成两个问题:得到最大集合size,输出这个集合 * 对于第一个问题,最大集合size就是dp数组的最大值,可以边画表边维护一个当前最大值; * 对于第二个问题,我们要维护一个parent数组,记录nums[i]加入到了哪个集合; * dp[i] = max(dp[i], dp[j] + 1

【Leetcode】Largest Divisible Subset

题目链接:https://leetcode.com/problems/largest-divisible-subset/ 题目: 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

【leetcode】368. Largest Divisible Subset

题目描述: 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. 解题分析: 如果a%b==0,则a=mb,

Leetcode 368. Largest Divisible Subset

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] Re

Largest Divisible Subset -- LeetCode

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] Re

[LintCode] Largest Divisible Subset

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 Given nums = [1,2,3

Largest Divisible Subset

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] Re