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 ...
随机推荐
- POJ 2101 Intervals 差分约束
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27746 Accepted: 10687 Description You ...
- Codeforces 631A Interview【模拟水题】
题意: 模拟模拟~~ 代码: #include<iostream> using namespace std; const int maxn = 1005; int a[maxn], b[m ...
- [bzoj4519][Cqoi2016]不同的最小割_网络流_最小割_最小割树
不同的最小割 bzoj-4519 Cqoi-2016 题目大意:题目链接. 注释:略. 想法: 我们发现这和最小割那题比较像. 我们依然通过那个题说的办法一样,构建最小割树即可. 接下来就是随便怎么处 ...
- POJ2632 Crashing Robots 解题报告
Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is neede ...
- component and slot
component and slot 使用: 1.component panel <article class="message"> <div class=&qu ...
- java编程思想-复用类
/* 一个文件中只能有一个public类 并且此public类必须与文件名相同 */ class WaterSource { private String s; WaterSource() { Sys ...
- zabbix学习系列之基础概念
触发器 概念 "监控项"仅负责收集数据,而通常收集数据的目的还包括在某指标对应的数据超出合理范围时给相关人员发送警告信息,"触发器"正式英语为监控项所收集的数据 ...
- Java中Comparator接口和Comparable接口的使用
普通情况下在实现对对象元素的数组或集合进行排序的时候会用到Comparator和Comparable接口,通过在元素所在的类中实现这两个接口中的一个.然后对数组或集合调用Arrays.sort或者Co ...
- HDMI接口基础知识及硬件设计
参考资料:http://blog.csdn.net/u013625961/article/details/53434189: http://blog.csdn.net/u014276460/artic ...
- Python数据导入
1.csv格式数据导入 import pandas as pd w=pd.read.csv("数据地址") w.describe() w.sort_values(by=" ...