368. 最大整除子集(368. Maximum divisible subset)

给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一元素对 (answer[i], answer[j]) 都应当满足:
answer[i] % answer[j] == 0 ,或
answer[j] % answer[i] == 0
如果存在多个有效解子集,返回其中任何一个均可。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/largest-divisible-subset
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class Solution {
    public List<Integer> largestDivisibleSubset(int[] nums) {
        if (nums == null || nums.length == 0) {
            return Collections.emptyList();
        }
        Arrays.sort(nums);
        int[] dp = new int[nums.length];
        Arrays.fill(dp, 1);
        int max = 1, maxIndex = 0;
        for (int i = 0; i < nums.length; ++i) {
            for (int j = 0; j < i; ++j) {
                if (nums[i] % nums[j] == 0) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                    if (dp[i] > max) {
                        max = dp[i];
                        maxIndex = i;
                    }
                }
            }
        }
        List<Integer> ans = new ArrayList<>();

        for (int i = maxIndex; i >= 0; --i) {
            if (nums[maxIndex] % nums[i] == 0 && dp[i] == max - ans.size()) {
                ans.add(nums[i]);
                maxIndex = i;
            }
        }
        Collections.reverse(ans);
        return ans;
    }
}
————————

Give you a set num composed of non repeating positive integers. Please find and return the largest integer division subset answer. Each element pair (answer [i], answer [J]) in the subset should meet:
Answer [i]% answer [J] = = 0, or
answer[j] % answer[i] == 0
If there are multiple valid solution subsets, any of them can be returned.

Source: leetcode
Link: https://leetcode-cn.com/problems/largest-divisible-subset
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class Solution {
    public List<Integer> largestDivisibleSubset(int[] nums) {
        if (nums == null || nums.length == 0) {
            return Collections.emptyList();
        }
        Arrays.sort(nums);
        int[] dp = new int[nums.length];
        Arrays.fill(dp, 1);
        int max = 1, maxIndex = 0;
        for (int i = 0; i < nums.length; ++i) {
            for (int j = 0; j < i; ++j) {
                if (nums[i] % nums[j] == 0) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                    if (dp[i] > max) {
                        max = dp[i];
                        maxIndex = i;
                    }
                }
            }
        }
        List<Integer> ans = new ArrayList<>();

        for (int i = maxIndex; i >= 0; --i) {
            if (nums[maxIndex] % nums[i] == 0 && dp[i] == max - ans.size()) {
                ans.add(nums[i]);
                maxIndex = i;
            }
        }
        Collections.reverse(ans);
        return ans;
    }
}