Leetcode 368. 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]

这种类似求最大值的题一般用DP来解。首先排序,这样就可以只验证后面的数是不是可以整除前面的数。用DP list来保存从一开头到当前这个数字的最长subset的长度,但是由于本题需要返回的是一个list,我们用pre来保存在当前最长subset中上一个元素的位置。

 1 class Solution(object):
 2     def largestDivisibleSubset(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: List[int]
 6         """
 7         nums.sort()
 8         n = len(nums)
 9         if n == 0:
10             return []
11         elif n ==1:
12             return nums
13         dp = [1]*n
14         pre = [None]*n
15
16         for i in range(n):
17             for j in range(i):
18                 if nums[i]% nums[j] == 0 and dp[j] +1 > dp[i]:
19                     dp [i] = dp[j] + 1
20                     pre[i] = j
21
22         idx = dp.index(max(dp))
23         ans = []
24         while idx is not None:
25             ans.append(nums[idx])
26             idx = pre[idx]
27         return ans
时间: 2024-11-04 06:30:07

Leetcode 368. Largest Divisible Subset的相关文章

368. Largest Divisible Subset

/* * 368. Largest Divisible Subset * 2016-7-16 by Mingyang * 和LIS很相似,dp[i]表示nums数组从0到i的最大集合的size. * 这题应该分成两个问题:得到最大集合size,输出这个集合 * 对于第一个问题,最大集合size就是dp数组的最大值,可以边画表边维护一个当前最大值; * 对于第二个问题,我们要维护一个parent数组,记录nums[i]加入到了哪个集合; * dp[i] = max(dp[i], dp[j] + 1

【Leetcode】Largest Divisible Subset

题目链接: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

【leetcode】368. 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. 解题分析: 如果a%b==0,则a=mb,

368.[LeetCode] Largest Divisible Subset

条件:动态规划 当nums[j]%nums[i]==0时,dp[i] = max(dp[i], dp[j]+1) 为了返回数组,使用pair记录路径 class Solution { public: vector<int> largestDivisibleSubset(vector<int>& nums) { //变量和初始值的定义 vector<int> res; if(nums.size()<1) return res;//临界条件 sort(nums

[LeetCode] 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] Re

Leetcode: 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] Re

Largest Divisible Subset -- LeetCode

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] Re

[LintCode] 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 Given nums = [1,2,3

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] Re