Classic DP solution similar to LIS, O(n^2)
Use DP to track max Set and pre index.\n\n    public class Solution {\n        public List largestDivisibleSubset(int[] nums) {\n            int n = nums.length
Use DP to track max Set and pre index.\n\n    public class Solution {\n        public List<Integer> largestDivisibleSubset(int[] nums) {\n            int n = nums.length;\n            int[] count = new int[n];\n            int[] pre = new int[n];\n            Arrays.sort(nums);\n            int max = 0, index = -1;\n            for (int i = 0; i < n; i++) {\n                count[i] = 1;\n                pre[i] = -1;\n                for (int j = i - 1; j >= 0; j--) {\n                    if (nums[i] % nums[j] == 0) {\n                        if (1 + count[j] > count[i]) {\n                            count[i] = count[j] + 1;\n                            pre[i] = j;\n                        }\n                    }\n                }\n                if (count[i] > max) {\n                    max = count[i];\n                    index = i;\n                }\n            }\n            List<Integer> res = new ArrayList<>();\n            while (index != -1) {\n                res.add(nums[index]);\n                index = pre[index];\n            }\n            return res;\n        }\n    }
['Dynamic Programming', 'Java']