【LEETCODE】368- Largest Divisible Subset [Python]

题目:

https://leetcode.com/problems/largest-divisible-subset/

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0.

If there are multiple solutions, return any subset is fine.

Example 1:

nums: [1,2,3]

Result: [1,2] (of course, [1,3] will also be ok)
Example 2:

nums: [1,2,4,8]

Result: [1,2,4,8]

题意:

Given a sequence of positive integers, find the longest subsequence which satisfies that for every two integers Si and Sj in it, there must be Si%Sj==0 or Sj%Si==0.

分析:

Step 1: The structure

We can notice that, if c%b==0, and b%a==0, there must be c%a==0, so we can firstly sort the sequence from smallest to largest element.

And let dp[i] denote the longest subsequence’s length among elements from num[i] to num[end].

Then if j>i and num[j] % num[i]==0, the elements behind j which %num[j]==0, can also %num[i]==0, so dp[i]=dp[j]+1.

But, there maybe several branches which can %num[i]==0,
for example: [1,2,4,8,10,20,40,80],
behind ‘2’, there are two branches, [4,8] and [10,20,40,80],
and we need to find the longest at point ‘2’,
so we should add the judgement, if dp[i]

Step 2: A recursive solution

dp[i] is used to store the longest length, but the target output is a sequence, so for the longest subsequence, we should use a variable ‘max_index’ to record the Start Point, and a list ‘child’ to store the next adjacent index since from Start Point in the subsequence. At the same time, we can also use a variable to record the max length, but it’s not necessary.

    for i in range(n-1, -1, -1):
        for j in range(i, n):
            if nums[j] % nums[i] == 0 and dp[i] < dp[j]+1:
                dp[i] = dp[j]+1

Step 3: Computing the optimal costs

class Solution(object):
    def largestDivisibleSubset(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """

        n = len(nums)

        if n==0 or n==1:
            return nums

        nums = sorted(nums)

        dp = [0]*n
        max = 0
        max_index = 0
        child = [0]*n
        result = []

        for i in range(n-1, -1, -1):
            for j in range(i, n):
                if nums[j] % nums[i] == 0 and dp[i] < dp[j]+1:
                    dp[i] = dp[j]+1
                    child[i] = j

                    # To store max subset length and relative start point index
                    if max < dp[i]:
                        max = dp[i]
                        max_index = i

        # Construct output subset
        i=max_index       
        for j in range(max):
            result.append(nums[i])
            i=child[i]

        return result

Notice:

If nums==[] or length is 1, return itself.
Don’t forget to sort before dp.
‘=’ and ‘==’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值