368. Largest Divisible Subset
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
vector<int> dp(nums.size(),0); //dp[i] 代表 nums[i]在nums里面能够整除的数字个数 1,2,3, 里面 dp[2]代表index=2的数 3 能够整数的数有1个;
vector<int> idx(nums.size(),0); //记录每个索引
if(nums.empty())return vector<int>();
sort(nums.begin(),nums.end()); //小到大排序
int imax=0,index=-1;
dp[0]=1;
for(int i=0;i<nums.size();++i)
{
dp[i]=1; //重要, 每个数i能被自己整除, 所以初始化为1
idx[i]=-1; //重要, 每个i位置上的索引初始化为-1代表没有被选中, 不赋值导致下面死循环
for(int j=i-1;j>=0;--j)
{
if(nums[i]%nums[j]==0) // 判断 i/j 是否整除
{
if(1+dp[j]>dp[i]) //循环1到j 找出 dp [1->j]里面最大的数
{
dp[i]=dp[j]+1; //既然j能整除的数字有dp[j]个, 而i又能整除j, 毫无疑问i也能整除j能整除的所有数字, dp[i]=dp[j]+1
idx[i]=j; //更新索引为j, 可能有很多个符合条件的j, 这里只能记录第一个
}
}
}
if(dp[i]>imax) //imax记录目前为止dpi的最大值
{
imax=dp[i];
index=i;
}
}
vector<int> r;
while(index!=-1) //循环完后index是最后一个要被加入结果集的索引
{
r.push_back(nums[index]);
index=idx[index]; //相当于从后面往前面遍历
}
return r;
}
};
368. Largest Divisible Subset的更多相关文章
- Leetcode 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 -- 找出一个数组使得数组内的数能够两两整除
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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- 368 Largest Divisible Subset 最大整除子集
给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0.如果有多个目标子集,返回其中任何一个均 ...
- LeetCode "Largest Divisible Subset" !
Very nice DP problem. The key fact of a mutual-divisible subset: if a new number n, is divisible wit ...
- [LeetCode] Largest Divisible Subset 最大可整除的子集合
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- Largest Divisible Subset
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 ...
随机推荐
- python 捕获异常顺序
catch 异常的时候,有关的异常(若是抛出子类异常,则父类异常的except也算.反之不算)except的语句是按代码顺序执行, 也就是说,当一个异常发生时,从若干except中若遇见异常类基类,父 ...
- Windows Server 2008 R2 服务器系统安装及配置全过程图文详解
前言 本文主要介绍了 windows Server 2008 R2 服务器系统的安装及相关配置. 介绍的是以优盘的方式安装. 写这篇博文的目的一来是为了供有需要的网友参考, 二来自己也在此做个记载. ...
- LeetCode算法历程-02
编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 输出: true ...
- 解决eclipse的自动换行问题。
安装方法:使用Eclipse 的自动升级功能,菜单栏选Help → install new Software 点解Add按钮,在“ Name ”中填入“ wordwrap ”,“ URL ”中填入“ ...
- php留言板程序
=================== 先创建note.php <html><head><title>PHP留言本</title></head&g ...
- axios,vue-axios在项目中的应用
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中 关于axios的功能: 1,从浏览器中创建XMLHttpRequests 2,从node.js常见Htt ...
- java面向对象编程(七)--四大特征之多态
1.多态概念 多态性是对象多种表现形式的体现.比如我们说"宠物"这个对象,它就有很多不同的表达或实现,比如有小猫.小狗.蜥蜴等等.那么我到宠物店说"请给我一只宠物&quo ...
- 注意Delphi 10.3.1中Trunc函数的问题
10.3.1,Trunc(0.35*100)=34,出现这种情况!bug?
- C16记技术服务支持
1.首页笔记:可以通过点击加号添加笔记的分类,还可以查看最近的10条笔记 2.图形:可以通过查看笔记的比例来分析自己最近的状况 3.快速添加笔记:点击添加笔记能够直接进行笔记 4.时间轴:能够通过文字 ...
- 字符串排序简单的工具类,数组转list,list转数组
public static void main(String[] args) { /* String[] str = {"a", "c", "b&qu ...