Wednesday, September 26, 2018

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



Analysis:
The idea is the same as LIS.



Code (Java):
class Solution {
    public List<Integer> largestDivisibleSubset(int[] nums) {
        List<Integer> ans = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return ans;
        }
        
        Arrays.sort(nums);
        
        int[] dp = new int[nums.length];
        int max = 1;
        for (int i = 0; i < nums.length; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                if (nums[i] % nums[j] == 0) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                    max = Math.max(max, dp[i]);
                }
            }
        }
        
        // print the largest set
        //
        int i = nums.length - 1;
        while (i >= 0 && dp[i] != max) {
            i--;
        }
        
        ans.add(nums[i]);
        i--;
        max--;
        
        while (i >= 0) {
            if ((ans.get(ans.size() - 1) % nums[i]) == 0 && dp[i] == max) {
                ans.add(nums[i]);
                max--;
            }
            i--;
        }
        
        
        return ans;
    }
}

No comments:

Post a Comment