Easy understood Java DP solution in 28ms with O(n^2) time
The basic idea is like:\n\n    1. Sort\n    2. Find the length of longest subset\n    3. Record the largest element of it.\n    4. Do a loop from the largest el
The basic idea is like:\n\n    1. Sort\n    2. Find the length of longest subset\n    3. Record the largest element of it.\n    4. Do a loop from the largest element to nums[0], add every element belongs to the longest subset.\n\n\nThe old version cant pass the test case [1,2,4,8,9,72] and [4,8,10,240], thanks for that @Yanning and @svc \nHere comes the revised version: \n\n    public static List<Integer> largestDivisibleSubset(int[] nums) {\n        List<Integer> res = new ArrayList<Integer>();\n        if (nums == null || nums.length == 0) return res;\n        Arrays.sort(nums);\n        int[] dp = new int[nums.length];\n        dp[0] = 1;\n\n        //for each element in nums, find the length of largest subset it has.\n        for (int i = 1; i < nums.length; i++){\n            for (int j = i-1; j >= 0; j--){\n                if (nums[i] % nums[j] == 0){\n                    dp[i] = Math.max(dp[i],dp[j] + 1);\n                }\n            }\n        }\n\n        //pick the index of the largest element in dp.\n        int maxIndex = 0;\n        for (int i = 1; i < nums.length; i++){\n            maxIndex = dp[i] > dp[maxIndex] ?  i :  maxIndex;\n        }\n\n        //from nums[maxIndex] to 0, add every element belongs to the largest subset.\n        int temp = nums[maxIndex];\n        int curDp = dp[maxIndex];\n        for (int i = maxIndex; i >= 0; i--){\n            if (temp % nums[i] == 0 && dp[i] == curDp){\n                res.add(nums[i]);\n                temp = nums[i];\n                curDp--;\n            }\n        }\n        return res;\n    }
[]