leetcode - 368. Largest Divisible Subset

Description

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.

Example 1:

Input: nums = [1,2,3]
Output: [1,2]
Explanation: [1,3] is also accepted.

Example 2:

Input: nums = [1,2,4,8]
Output: [1,2,4,8]

Constraints:

1 <= nums.length <= 1000
1 <= nums[i] <= 2 * 10^9
All the integers in nums are unique.

Solution

Solved after help…

Use dp[i] to keep track of the length of longest subset that ends with nums[i], then the transformation equation is:
d p [ i ] = 1 + max ⁡ ( d p [ j ] ) , ∀    n u m s [ i ] % n u m s [ j ] = = 0 dp[i] = 1 + \max(dp[j]), \forall \;nums[i] \% nums[j] == 0 dp[i]=1+max(dp[j]),nums[i]%nums[j]==0

Because we need to return the longest subset, so when updating the dp, we add another dimension to remember j that we finally choose.

Time complexity: o ( n 2 ) o(n^2) o(n2)
Space complexity: o ( n ) o(n) o(n)

Code

class Solution:
    def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
        dp = [[1, i] for i in range(len(nums))]
        nums.sort()
        for i in range(1, len(nums)):
            for j in range(i):
                if nums[i] % nums[j] == 0:
                    if dp[j][0] + 1 > dp[i][0]:
                        dp[i][0] = dp[j][0] + 1
                        dp[i][1] = j
        max_len = 1
        res = [nums[dp[0][1]]]
        for i in range(len(dp)):
            if dp[i][0] > max_len:
                res = []
                max_len = dp[i][0]
                index = dp[i][1]
                start_i = i
                while index != start_i:
                    res.append(nums[start_i])
                    start_i = index
                    index = dp[start_i][1]
                res.append(nums[start_i])
        return res

相关推荐

  1. leetcode - 368. Largest Divisible Subset

    2024-02-11 06:06:02       42 阅读
  2. LeetCode 36

    2024-02-11 06:06:02       35 阅读
  3. LeetCode 367, 56, 22

    2024-02-11 06:06:02       23 阅读
  4. LeetCode //C - 338. Counting Bits

    2024-02-11 06:06:02       56 阅读
  5. Leetcode 367. Valid Perfect Square

    2024-02-11 06:06:02       44 阅读
  6. leetcode - 362. Design Hit Counter

    2024-02-11 06:06:02       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-11 06:06:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-11 06:06:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-11 06:06:02       82 阅读
  4. Python语言-面向对象

    2024-02-11 06:06:02       91 阅读

热门阅读

  1. 从零开始:用 Rust 编写你的第一个 Web 服务

    2024-02-11 06:06:02       45 阅读
  2. 如何为Kafka加上账号密码(二)

    2024-02-11 06:06:02       52 阅读
  3. C#系列-C#访问MongoDB+redis+kafka(7)

    2024-02-11 06:06:02       51 阅读
  4. 最小生成树——Prim/Kruskal Python

    2024-02-11 06:06:02       51 阅读
  5. 迟旧迎新感悟

    2024-02-11 06:06:02       43 阅读
  6. MySQL中drop、delete与trancate的区别

    2024-02-11 06:06:02       46 阅读
  7. mysql RR、RC隔离级别实现原理

    2024-02-11 06:06:02       43 阅读
  8. 【PyTorch】张量(Tensor)的生成

    2024-02-11 06:06:02       48 阅读
  9. 最关键的十个图像特征

    2024-02-11 06:06:02       41 阅读