-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0368-largest-divisible-subset.js
More file actions
36 lines (33 loc) · 956 Bytes
/
0368-largest-divisible-subset.js
File metadata and controls
36 lines (33 loc) · 956 Bytes
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
/**
* 368. Largest Divisible Subset
* https://leetcode.com/problems/largest-divisible-subset/
* Difficulty: Medium
*
* Given a set of distinct positive integers nums, return the largest subset answer such that
* every pair (answer[i], answer[j]) of elements in this subset satisfies:
* - answer[i] % answer[j] == 0, or
* - answer[j] % answer[i] == 0
*
* If there are multiple solutions, return any of them.
*/
/**
* @param {number[]} nums
* @return {number[]}
*/
var largestDivisibleSubset = function(nums) {
nums.sort((a, b) => a - b);
const dp = nums.map(() => [1, -1]);
let max = 0;
for (let i = 1; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[i] % nums[j] === 0 && dp[j][0] + 1 > dp[i][0]) {
dp[i] = [dp[j][0] + 1, j], dp[i][0] > dp[max][0] && (max = i);
}
}
}
const result = [];
for (let i = max; i >= 0; i = dp[i][1]) {
result.unshift(nums[i]);
}
return result;
};