813. Divisible Chain
A toy maker has gears whose tooth counts are listed in nums. Gears are placed on one shaft, and the maker wants a set of gears where, for every two chosen gears, the smaller tooth count divides the larger one exactly (equal tooth counts divide each other).
Return the largest number of gears that can be chosen from the list. The list may contain repeated values; each listed gear can be used at most once, so two gears with equal tooth counts both count.
Aim for time O(n^2) and extra space O(n), where n is the number of gears.
Example 1
- Input:
- nums = [12,4,36,6,2,18]
- Output:
- 4
- Explanation:
The values 2, 6, 18, 36 form a chain where each divides the next, and no chain of five values exists.
Example 2
- Input:
- nums = [10,15,25,5]
- Output:
- 2
- Explanation:
5 divides each of 10, 15 and 25, but those three do not divide one another, so the best chain has 2 values.
Constraints
1 ≤ nums.length ≤ 1200
1 ≤ nums[i] ≤ 2*109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,000 msC++ 250 msJava 500 msJavaScript 500 msTypeScript 500 ms
Expected complexity
- Time
- O(n^2)
- Space
- O(n)