资讯专栏INFORMATION COLUMN

Leetcode[368] Largest Divisible Subset

springDevBird / 658人阅读

摘要:让数组从小到大排序。因为如果一个数能被加到这个中的话,说明这个数能被这个中的最大的数整除。同样可以用一个数组来记录之前搜索过的。,表示的是我们搜索的路径是从到。初始化这个位置是头结点。说明是,并没有是当前最大的里的最大值。

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]

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

nums: [1,2,4,8]

Result: [1,2,4,8]

DP

复杂度
O(N^2),O(N)

思路
先要sort这个数组。让数组从小到大排序。因为如果一个数能被加到这个list中的话,说明这个数能被这个list中的最大的数整除。所以需要先sortlist。

同样可以用一个数组来记录之前搜索过的path。prev[i] = j,表示的是我们搜索的路径是从j到i。同样用来保存搜索路径的方法还有,用一个list然后进行backtracking,也可以用来记录。

同样的应用还可以用在union find上面。

代码

public List largestDivisibleSubset(int[] nums) {
    List res = new ArrayList();
    if(nums == null || nums.length == 0) return res;
    int[] dp = new int[nums.length]; 
    // 保留一个数组的里面的数的Path的方法,可以用数组存index这样做。
    int[] prev = new int[nums.length]; 
    Arrays.sort(nums);
    int max = 0, index = 0;
    for (int i = 0; i < nums.length; i++){
        dp[i] = 1;
        // 初始化这个位置是头结点。说明prev是-1,并没有parent node.
        prev[i] = -1;
        for (int j = i - 1; j >= 0; j--){
            if (nums[i] % nums[j] == 0 && dp[j] + 1 > dp[i]){
                dp[i] = dp[j] + 1;
                prev[i] = j;
            }
        }
        // dp[i]是当前最大的subset里的最大值。
        if (dp[i] > max){
            max = dp[i];
            index = i;
        }
    }
    while (index != -1){
        res.add(nums[index]);
        index = prev[index];
    }
    return res;
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65219.html

相关文章

  • leetcode368. Largest Divisible Subset

    摘要:题目要求假设有一组值唯一的正整数数组,找到元素最多的一个子数组,这个子数组中的任选两个元素都可以构成或。只要这个数字是前面数字的倍数,则构成的数组的长度则是之前数字构成最长子数组加一。 题目要求 Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj)...

    Honwhy 评论0 收藏0
  • 368. Largest Divisible Subset

    368. Largest Divisible Subset 题目链接:https://leetcode.com/problems... dp记录最大的长度,加parent指针存路径。dp方程是:dp[i] = max(dp[j]) + 1, if nums[i]%nums[j] == 0 public class Solution { public List largestDivisibl...

    mmy123456 评论0 收藏0
  • 368. Largest Divisible Subset

    摘要:题目解答参考的里的解法,核心思想从小到大,每一位数都能被比他大的数整除。对于从后往前看,找出每一个可以被它整除的数的数组,并更新它作为从这里开始,往后最大的,记录下最大数组开始的地方,并把下一个数记在里找出最长的这个数组中的每一个数 题目:Given a set of distinct positive integers, find the largest subset such th...

    source 评论0 收藏0
  • LeetCode[333] Largest BST Subtree

    摘要:复杂度思路考虑对于每一个节点来说,能组成的的。那么并且所以我们需要两个返回值,一个是这个是不是,另一个是当前的能组成的最大的值。代码这个能构成一个这个不能构成一个 LeetCode[333] Largest BST Subtree Given a binary tree, find the largest subtree which is a Binary SearchTree (B...

    WrBug 评论0 收藏0
  • [LeetCode/LintCode] Largest Palindrome Product

    Problem Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example Input: 2Output: 987Ex...

    Barry_Ng 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<