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:this

nums: [1,2,3]

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

 

Example 2:spa

nums: [1,2,4,8]

Result: [1,2,4,8]

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.code

If there are multiple solutions, return any subset is fine.blog

问题分析:排序

动态规划思路与LIS问题基本一致,首先将数组进行排序,而后依次遍历每个元素,肯定以该元素为结尾的LDS长度(寻找该元素前面的全部能够被整除的元素的LDS最大值,在此基础上加1)。ip

此时时间复杂度为基本代码以下:element

public class Solution {
    public List<Integer> largestDivisibleSubset(int[] nums) {
        List<Integer> rst = new LinkedList<>();
        if (nums == null || nums.length == 0) return rst;
        Arrays.sort(nums);
        rst.add(nums[0]);
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            List<Integer> temp = new LinkedList<>();
            temp.add(nums[i]);
            map.put(nums[i], temp);
        }
        for (int i = 1; i < nums.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] % nums[j] == 0 && map.get(nums[j]).size() + 1 > map.get(nums[i]).size()) {
                    List<Integer> temp = new LinkedList<>(map.get(nums[j]));
                    temp.add(nums[i]);
                    map.put(nums[i], temp);
                }
            }
            if (rst.size() < map.get(nums[i]).size()) rst = map.get(nums[i]);
        }
        return rst;
    }
}

以上代码AC了,可是效率很低。能够看出时间复杂度为O(n^2),空间复杂度为O(n^2)。get

问题优化:

上述解法空间复杂度很高,利用map直接把全部的结果都记录了下来。仔细思考会发现,能够经过记录每一步的上一个数组下标来将空间复杂度下降到O(n)。

改进后代码以下,其中rec数组用来记录以特定下标为最后一个元素的LDS长度,index数组用来记录以特定下标为最后一个元素的LDS的上一个元素下标。last_index用来记录最终结果的最后一个元素下标,max_length用来记录最终LDS长度。

public class Solution {
    public List<Integer> largestDivisibleSubset(int[] nums) {
        List<Integer> rst = new LinkedList<>();
        if (nums == null || nums.length == 0) return rst;
        int[] rec = new int[nums.length];
        int[] index = new int[nums.length];
        int last_index = 0;
        int max_length = 1;
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            rec[i] = 1;
            index[i] = -1;
        }
        for (int i = 1; i < nums.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] % nums[j] == 0 && rec[j] + 1 > rec[i]) {
                    rec[i] = rec[j] + 1;
                    index[i] = j;
                }
            }
            if (rec[i] > max_length) {
                max_length = rec[i];
                last_index = i;
            }
        }
        while (last_index >= 0) {
            rst.add(nums[last_index]);
            last_index = index[last_index];
        }
        return rst;
    }
}

这种解法时间复杂度为O(n^2),空间复杂度为O(n)。击败96.88%submissions。

相关文章
相关标签/搜索