作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/largest-divisible-subset/description/

题目描述:

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:

Input: [1,2,3]
Output: [1,2] (of course, [1,3] will also be ok)
Example 2: Input: [1,2,4,8]
Output: [1,2,4,8]

题目大意

找出一个数组中最长的可以互相整除的集合。互相整除的含义是,在数组中随便抽出两个数字,其中一个是另一个的约数。

解题方法

是否想起了Longest Increase Sequence?这两个题非常相似啊,所以做题一定要把融会贯通才行。

首先需要对题目给出的数组进行排序,这样的作用是我们从左到右遍历一次,每次只看后面的数字能不能被前面的整除就行。

问题分成了两个部分:

  1. 寻找最长的满足题目的数组
  2. 输出整个结果

使用一个一维DP,其含义是题目要求的数组,DP[i]的含义是,从0~i位置满足题目的最长数组。先用i遍历每个数字,然后用j从后向前寻找能被nums[i]整除的数字,这样如果判断能整除的时候,再判断dp[i] < dp[j] + 1,即对于以i索引结尾的最长的数组是否变长了。在变长的情况下,需要更新dp[i],同时使用parent[i]更新i的前面能整除的数字。另外还要统计对于整个数组最长的子数组长度。

知道了对于每个位置最长的子数组之后,我们也就知道了对于0~n区间内最长的满足题目条件的数组,最后需要再次遍历,使用parent就能把正儿个数组统计输出出来。因为这个最大的索引mx_index是对n而言的,所以输出是逆序的。

总感觉语言是乏力的,明白LIS对这个题有好处。

注意

注意这个case:
[1,2,4,8,9,72]
到72的时候,往前找到9,可以整除,更新dp[5]为max(1, 2 + 1) = 3,
注意此时应该继续往前找,不能停,直到找到8,发现dp[3] + 1 = 5 > 3,于是更新dp[i]
注意就是不能停,找到一个能整除并不够,前面有可能有更大的啊~~

时间复杂度是O(N^2),空间复杂度是O(N).

代码如下:

class Solution:
def largestDivisibleSubset(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if not nums: return []
N = len(nums)
nums.sort()
dp = [0] * N #LDS
parent = [0] * N
mx = 0
mx_index = -1
for i in range(N):
for j in range(i - 1, -1 , -1):
if nums[i] % nums[j] == 0 and dp[i] < dp[j] + 1:
dp[i] = dp[j] + 1
parent[i] = j
if dp[i] > mx:
mx = dp[i]
mx_index = i
res = list()
for k in range(mx + 1):
res.append(nums[mx_index])
mx_index = parent[mx_index]
return res[::-1]

参考资料:

https://segmentfault.com/a/1190000005922634
http://www.cnblogs.com/grandyang/p/5625209.html

日期

2018 年 10 月 12 日 —— 又要到周末了!

【LeetCode】368. Largest Divisible Subset 解题报告(Python)的更多相关文章

  1. Leetcode 368. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  2. 【leetcode】368. Largest Divisible Subset

    题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...

  3. 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  4. LeetCode 812 Largest Triangle Area 解题报告

    题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...

  5. LeetCode 976 Largest Perimeter Triangle 解题报告

    题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...

  6. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  7. 368 Largest Divisible Subset 最大整除子集

    给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0.如果有多个目标子集,返回其中任何一个均 ...

  8. 368. Largest Divisible Subset

    class Solution { public: vector<int> largestDivisibleSubset(vector<int>& nums) { vec ...

  9. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. 搭建FastDFS集群

    先插一张图(学习的时候找的)http://blog.csdn.net/u012453843/article/details/68957209?> 软件下载地址:主要是fastdfs.libfas ...

  2. 二进制免编译My SQL

    一 下载 MySQL 安装包教程 https://blog.csdn.net/zhan107876/article/details/100701135 ll -h mysql-5.6.47-linux ...

  3. 网站性能调优实战-学相伴KuangStudy

    面对并发我们是如何优化KuangStudy网站性能的? 每个项目都会随着用户和数据的增长调整架构,来面对未来的问题,我们也不例外,在1月5号我们平台正式公测后,引起了很多观众的热烈反响,仅仅4天,注册 ...

  4. A Child's History of England.33

    To strengthen his power, the King with great ceremony betrothed his eldest daughter Matilda, then a ...

  5. Spring标签库

    spring提供了两个标签库文件:spring-form.tld(表单标签库,用于输出HTML表单)  spring.tld(基础标签库,用于Spring数据绑定等) 使用步骤: 1,配置表单标签库, ...

  6. Mysql多字段模糊查询

    MySQL同一字段多值模糊查询 一. 同一字段多值模糊查询,使用多个or进行链接,效率不高,但没有更好的解决方案.(有看到CHARINDEX 关键字,可查询结果并不是模糊,举个栗子 例如SELECT ...

  7. 【编程思想】【设计模式】【行为模式Behavioral】Specification

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/specification.py #!/usr/bin/e ...

  8. 【Java 调优】Java性能优化

    Java性能优化的50个细节(珍藏版) 1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: ...

  9. 1.Java语言基础

    一:java语言介绍 (1). 1991年出现,1995年5月正式发布 出生地:SUN  创始人:James Gosling  2009年4月被Oracle收购 目前最新的版本2018年3月v10.0 ...

  10. Mysql原有环境部署多个版本

    目录 一.环境准备 二.下载安装包 三.Mysql-5.7单独部署 四.启动Mysql-5.7 五.muliti使用 一.环境准备 原先已经有一个5.6版本的数据库在运行了,当前操作是完全不影响原数据 ...