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中上一个元素的位置。
class Solution(object):
def largestDivisibleSubset(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
nums.sort()
n = len(nums)
if n == 0:
return []
elif n ==1:
return nums
dp = [1]*n
pre = [None]*n for i in range(n):
for j in range(i):
if nums[i]% nums[j] == 0 and dp[j] +1 > dp[i]:
dp [i] = dp[j] + 1
pre[i] = j idx = dp.index(max(dp))
ans = []
while idx is not None:
ans.append(nums[idx])
idx = pre[idx]
return ans
Leetcode 368. Largest Divisible Subset的更多相关文章
- 【LeetCode】368. Largest Divisible Subset 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- 【leetcode】368. Largest Divisible Subset
题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...
- 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- 368 Largest Divisible Subset 最大整除子集
给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0.如果有多个目标子集,返回其中任何一个均 ...
- 368. Largest Divisible Subset
class Solution { public: vector<int> largestDivisibleSubset(vector<int>& nums) { vec ...
- [LeetCode] Largest Divisible Subset 最大可整除的子集合
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- LeetCode "Largest Divisible Subset" !
Very nice DP problem. The key fact of a mutual-divisible subset: if a new number n, is divisible wit ...
- Largest Divisible Subset -- LeetCode
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- [Swift]LeetCode368. 最大整除子集 | Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
随机推荐
- Symbol ES6 新增的一种值类型数据,表示一种绝不重复的值
let s1 = Symbol(33); let s2 = Symbol(33); alert(typeof(s1)); //数据类型判断 // alert(s1.toString()); //可把一 ...
- PAT 1018. 锤子剪刀布 (20)
现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输入第1行给出正整数N(<=105),即双方交锋的次数.随后N行,每行给出一次交锋的信息,即 ...
- 如何将matlab画出的图片保存为要求精度
· 来源:http://emuch.net/bbs/viewthread.php?tid=2705843 杂志社对投稿图片的分辨率通常有如下要求: TIFF: Colour or greyscale ...
- BZOJ 1082 【SCOI2005】 栅栏
Description 农夫约翰打算建立一个栅栏将他的牧场给围起来,因此他需要一些特定规格的木材.于是农夫约翰到木材店购买木材.可是木材店老板说他这里只剩下少部分大规格的木板了.不过约翰可以购买这些木 ...
- Tomcat部署学习
tomcat也可以称为catalina catalina_home就是tomcat安装路径:D:\Program Files\apache-tomcat-8.0.36\bin windows下 ...
- linux | 管道符、输出重定向
1 输出重定向 ll > a.txt 将 ll的结果写入到a.txt 2 管道符 ls -la | grep h* 这条命令的理解为:ls -la 的结果作为gerp h* 的结果 gerp 是 ...
- java swing模仿随机频谱
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Grap ...
- Android AlertDialog
在Android 4.2 中不推荐使用showDialog弹窗,这里简单总结一下AlertDialog的使用方法,以后复习的时候看着方便,详细使用方法需要的时候再研究. setTitle :为对话框设 ...
- ListView适配器获取布局文件作为View的三种方式
第一种方法: public View getView(int position, View convertView, ViewGroup parent) { View view = null; if ...
- [Google Guava]学习--新集合类型BiMap
BiMap提供了一种新的集合类型,它提供了key和value的双向关联的数据结构. Bimap 能非常方便的实现map<key,value>的转置要求,也就是value变为key,key变 ...