Problem statement

https://binarysearch.com/problems/Dividing-Station/

Solution

Equal to Leetcode 0368. Largest Divisible Subset.

Complexity

It is O(n^2) for time and space.

Code

class Solution:
    def solve(self, nums):
        if not nums: return 0
        nums.sort()
        dp = [1] * len(nums)
        for i in range(len(nums)):
            for j in range(i):
                if nums[i] % nums[j] == 0:
                    dp[i] = max(dp[i], dp[j] + 1)
        return max(dp)