-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
36 lines (32 loc) · 851 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Problem: https://leetcode.com/problems/largest-divisible-subset/description/
*/
/**
* @param {number[]} nums
* @return {number[]}
*/
var largestDivisibleSubset = function(nums) {
if (!nums.length) return [];
nums.sort(function (a, b) {
return a > b ? 1 : -1;
});
var maxLength = 0, dp = {};
nums.forEach(function (num, index) {
dp[num] = { max: 1 };
for (var i = index - 1; i >= 0; --i) {
if (0 === num % nums[i] && dp[nums[i]].max >= dp[num].max - 1) {
dp[num].max = dp[nums[i]].max + 1;
dp[num].pre = i;
}
}
dp[num].arr = dp[num].pre === undefined ? [num] : [].concat(dp[nums[dp[num].pre]].arr, num);
});
var max = 0, result;
nums.forEach(function (num) {
if (dp[num].max > max) {
max = dp[num].max;
result = dp[num].arr;
}
});
return result;
};