Problem 563227 · medium · Phase 05 Advanced Algorithms & Graphs

Divisor Tower

dynamic programming · 1-D dp · sorting · divisibility · longest chain

A puzzle maker stacks numbered blocks into a tower. The tower is stable when, for every pair of blocks in it, the smaller number divides the larger one. Given the distinct positive numbers nums on the available blocks, return the largest number of blocks a stable tower can contain (0 if there are no blocks).

Examples

Input:  nums = [3, 5, 10, 20, 6, 40]
Output: 4
Explanation: {5, 10, 20, 40}; every smaller member divides every larger one.

Input:  nums = [7]
Output: 1

Constraints

  • 0 <= len(nums) <= 3000, all values distinct
  • 1 <= nums[i] <= 10**9
  • Target complexity: O(n^2) time; checking every subset is exponential.

Goals

  • Reduce a pairwise condition to a chain condition by sorting
  • Compute the longest chain ending at each element
Starting Python…