【LeetCode】368. Largest Divisible Subset 解题报告(Python)
作者: 负雪明烛
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?这两个题非常相似啊,所以做题一定要把融会贯通才行。
首先需要对题目给出的数组进行排序,这样的作用是我们从左到右遍历一次,每次只看后面的数字能不能被前面的整除就行。
问题分成了两个部分:
- 寻找最长的满足题目的数组
- 输出整个结果
使用一个一维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)的更多相关文章
- Leetcode 368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- 【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 ...
- 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 ...
- LeetCode 976 Largest Perimeter Triangle 解题报告
题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 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】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
随机推荐
- 关于AnnotationHub的一些应用
AnnotationHub是一个包含大量注释信息的数据库,里面有很多物种,以及来源于很多数据库的注释信息. 1,安装这个包 source("https://bioconductor.org/ ...
- LVS-三种模式的配置详情
NAT模式 实验环境 LVS1 VIP 192.168.31.66 DIP 192.168.121.128 WEB1 192.168.121.129 WEB2 192.168.121.130 安装与配 ...
- JAVA写入TXT
用java生成txt文件有两种方式: 1)是通过字符流(或字节流): 2)是直接调用PrintWriter类. 具体实现过程如下: 1)字符流(字节流) 代码如下: import java.io.Fi ...
- 学习java 7.4
学习内容:遍历字符串要点:for(int i = 0;i < line.length();i++) { System.out.println(line.chatAt(i)); } 字符串拼接: ...
- 日常Java 2021/9/26 (二柱升级版)
package m; import java.util.Scanner;import java.util.Random; public class di_er { static int number= ...
- Hadoop fs.copyToLocalFile错误
fs.copyToLocalFile(new Path("/study1/1.txt"), new Path("C:/Users/Administrator/Deskto ...
- 优化if else嵌套代码
写在前面 不知大家有没遇到过像"横放着的金字塔"一样的if else嵌套: if (true) { if (true) { if (true) { if (true) { if ( ...
- 【分布式】Zookeeper客户端基本的使用
与mysql.redis等软件一样,zookeeper的软件包中也提供了客户端程序用于对服务器上的数据进行操作.本节我们就来学习zookeeper客户端的使用方法.不过在详细讲解zk客户端的使用方法之 ...
- OkHttp3 使用
导入 compile 'com.squareup.okhttp3:okhttp:3.3.0' GET请求 String url = "https://www.baidu.com/" ...
- Camera、音频录制与Vitamio框架
一.Camera 1.概述 Android框架包含了各种相机哥相机功能的支持,是你可以在应用中捕获图像和视频. 在应用能使用设备上的相机之前,先想一想将来会如何使用此硬件: (1)Camera 应该 ...