资讯专栏INFORMATION COLUMN

leetcode368. Largest Divisible Subset

Honwhy / 3436人阅读

摘要:题目要求假设有一组值唯一的正整数数组,找到元素最多的一个子数组,这个子数组中的任选两个元素都可以构成或。只要这个数字是前面数字的倍数,则构成的数组的长度则是之前数字构成最长子数组加一。

题目要求
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:

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

Input: [1,2,4,8]
Output: [1,2,4,8]

假设有一组值唯一的正整数数组,找到元素最多的一个子数组,这个子数组中的任选两个元素都可以构成Si % Sj = 0 或 Sj % Si = 0。

思路和代码

这题最核心的思路在于,假如知道前面k个数字所能够组成的满足题意的最长子数组,我们就可以知道第k+1个数字所能构成的最长子数组。只要这个数字是前面数字的倍数,则构成的数组的长度则是之前数字构成最长子数组加一。

这里我们使用了两个临时数组count和pre,分别用来记录到第k个位置上的数字为止能够构成的最长子数组,以及该子数组的前一个可以被整除的值下标为多少。

    public List largestDivisibleSubset(int[] nums) {
        int[] count = new int[nums.length];
        int[] pre = new int[nums.length];
        Arrays.sort(nums);
        int maxIndex = -1;
        int max = 0;
        for(int i = 0 ; i=0 ; j--) {
                if(nums[i] % nums[j] == 0 && count[j] >= count[i]){
                    count[i] = count[j] + 1;
                    pre[i] = j;
                }
            }
            if(count[i] > max) {
                max = count[i];
                maxIndex = i;
            }
        }
        
        List result = new ArrayList();
        while(maxIndex != -1){
            result.add(nums[maxIndex]);
            maxIndex = pre[maxIndex];
        }
        return result;
    }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

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

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

相关文章

  • Leetcode[368] Largest Divisible Subset

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

    springDevBird 评论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元查看
<