368 Largest Divisible Subset 最大整除子集
给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0。
如果有多个目标子集,返回其中任何一个均可。
示例 1:
集合: [1,2,3]
结果: [1,2] (当然, [1,3] 也正确)
示例 2:
集合: [1,2,4,8]
结果: [1,2,4,8]
详见:https://leetcode.com/problems/largest-divisible-subset/description/
C++:
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums)
{
sort(nums.begin(), nums.end());
vector<int> dp(nums.size(), 0), parent(nums.size(), 0), res;
int mx = 0, mx_idx = 0;
for (int i = nums.size() - 1; i >= 0; --i)
{
for (int j = i; j < nums.size(); ++j)
{
if (nums[j] % nums[i] == 0 && dp[i] < dp[j] + 1)
{
dp[i] = dp[j] + 1;
parent[i] = j;
if (mx < dp[i])
{
mx = dp[i];
mx_idx = i;
}
}
}
}
for (int i = 0; i < mx; ++i)
{
res.push_back(nums[mx_idx]);
mx_idx = parent[mx_idx];
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/5625209.html
368 Largest Divisible Subset 最大整除子集的更多相关文章
- 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 ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- 368. Largest Divisible Subset
class Solution { public: vector<int> largestDivisibleSubset(vector<int>& nums) { vec ...
- [Swift]LeetCode368. 最大整除子集 | 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 最大可整除的子集合
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
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
随机推荐
- Codeforces Round #489 (Div. 2) B、C
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
- cogs——644. 课程安排问题
644. 课程安排问题 ★ 输入文件:curriculum.in 输出文件:curriculum.out 简单对比时间限制:1 s 内存限制:128 MB 问题描述 一个软件专业的学生 ...
- Linux下重启加载命令
nginx -s reload
- Servlet发送邮件
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/sending-email.html: 使用Servlet发送一封电子邮件是非常简单的,但是开始之 ...
- Analyzing Storage Performance using the Windows Performance Analysis ToolKit (WPT)
https://blogs.technet.microsoft.com/robertsmith/2012/02/07/analyzing-storage-performance-using-the-w ...
- html5 编辑
在html中想获得矢量图形可以用svg标签.该标签画出的图形全部用代码实现. 可以用在线html编辑工具来进行所见即所得编辑,然后到处源码. 比较好用的工具有http://editor.method. ...
- 1.spring boot要求最低jdk1.8,平安默认1.6问题,-》安装JDK1.8 2.maven 3.3.3要求最低jdk1.7->安装jdk 1.8
1.spring boot要求最低jdk1.8,平安默认1.6问题,->安装JDK1.82.maven 3.3.3要求最低jdk1.7->安装jdk 1.8
- ABAP学习之旅——多种方式建立模块化功能
在ABAP中.有多种方法能够建立模块化的功能. 以下依次对其种三种进行介绍. 一. 使用子程序(Subroutine) 1. 基本的语法: FORM subname. ...
- android 获取手机信息工具类
package com.yqy.yqy_listviewheadview; import android.content.Context; import android.telephony.Telep ...
- java设计模式 -------- 行为模式 之 策略模式(4)
[本文是自己学习所做笔记.欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020] 上面3节实现了从最初的对整形数组排序到最后能够对全部类型都能够依据须要定义自 ...