1-D Dynamic Programming Increasing subsequences
530. Largest Divisible Subset
You are given an array nums of distinct positive integers. Return the largest subset of nums in which every
pair of numbers divides: for any two numbers a and b in the subset, a divides b or b divides a.
A number a divides b when b % a == 0. A subset is any choice of numbers from nums, in any
order. It does not have to keep the order of nums, and a single number on its own is a subset.
You may return the numbers in any order. Often several subsets share the largest size, and any of them is
correct. The result shows the size of the subset you return, as a divisible subset of size 4, when every
pair in it divides. Otherwise it shows what is wrong with it, such as 4 and 6 do not divide each other.
Example 1
Input: nums = [5, 20, 3, 10, 9, 40, 6]
[5,10,20,40]Explanation: Each of 5, 10 and 20 divides every larger number in the subset. Adding 3, 6 or 9 would break it, since 5 divides none of them.
Example 2
Input: nums = [2, 3, 7]
[2]Explanation: No number divides another, so the largest subsets hold one number each. [3] and [7] are also correct.
Example 3
Input: nums = [1, 1000000000, 2000000000, 3]
[1,1000000000,2000000000]Explanation: 1 divides every number, and 2000000000 is twice 1000000000. The number 3 does not divide 1000000000, so it cannot join them.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ nums.length ≤ 1041 ≤ nums[i] ≤ 2 × 109- All the numbers in
numsare distinct.
Clarifying questions
What you would ask an interviewer before writing any code.