【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 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

dp数组表示从0~i包括第i个元素最大的divisible subset size

pre数组用来标记 状态转移过程中的方向,用于回溯最大值时的解集。

dp[i]=max{dp[i],dp[j]+1}

算法:

    public List<Integer> largestDivisibleSubset(int[] nums) {
        LinkedList<Integer> res = new LinkedList<Integer>();
        if (nums.length == 0) {
            return res;
        }
        Arrays.sort(nums);

        int dp[] = new int[nums.length]; //记录从0~i包括nums[i]的最大subset的size
        int pre[] = new int[nums.length];//记录到当前元素最大size的前一位数的下标
        int maxIdx = -1, max = -1;

        for (int i = 0; i < nums.length; i++) { //初始化
            dp[i] = 1;
            pre[i] = -1;
        }

        for (int i = 1; i < nums.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] % nums[j] == 0 && dp[i] < dp[j] + 1) {
                    dp[i] = dp[j] + 1;
                    pre[i] = j;//
                }
            }
        }

        for (int i = 0; i < nums.length; i++) {//找到最大的子集size 和它最后元素的下标
            if (dp[i] > max) {
                max = dp[i];
                maxIdx = i;
            }
        }

        for (int i = maxIdx; i >= 0;) { //回溯解集
            res.addFirst(nums[i]);
            i = pre[i];
        }
        return res;
    }
时间: 2024-08-09 14:40:35

【Leetcode】Largest Divisible Subset的相关文章

【LeetCode】Largest Number 解题报告

[题目] Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of

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

【leetcode】Largest Number

Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string

【leetcode】Largest Rectangle in Histogram

题目信息如下: 1.题目地址为:https://leetcode.com/problems/largest-rectangle-in-histogram/ 2.题目意思为: 给定一个非负数组height,代表了矩形的高度(其中矩形宽度为1),求在其中能找出最大的矩形面积. 3.给的例子为: height = [2,1,5,6,2,3]. 输出为:10.示意图如下 那么下面为正文,讲讲算法的依据(以上面的例子为例): 1.第一个2就没什么讲的了,面积必然为2. 2.当指针指向height[1]的时

【leetcode】Largest Number ★

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an i

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,