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 ...
随机推荐
- webpack 简单配置
webpack.config.js const path = require('path'); const HtmlWebpackPlugin=require('html-webpack-plugin ...
- java中引用的概念
强引用(StrongReference) 强引用就是指在程序代码之中普遍存在的,比如下面这段代码中的object和str都是强引用: 1 2 Object object = new Object(); ...
- Mybatis第一天
Mybatis第一天 框架课程 1. 课程计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis ...
- lintcode 程序题
1500802025 才仁代吉 第一链表类参数是整形链表,广度优先遍历: 队列是先进先出的概念 所以使用于保存节点 2插树的话 1个节点有个2个子节点 先将根节点入队,然后访问根节点数据(此时让根节点 ...
- JS 清除DOM 中空白元素节点
HTML中的空白节点会影响整体的HTML的版面排榜 例如: 制作百度首页时,两个input之间的空白节点将本来是要整合在一起的搜索栏硬是把按钮和搜索框分离出现好丑的间隙 这时我们就可以用js清除这个空 ...
- selenium 分布式 [WinError 10061] 由于目标计算机积极拒绝
selenium grid分布式,老是出现[WinError 10061] 由于目标计算机积极拒绝的问题 网上查了一圈,出现积极拒绝大概是代理问题, 捣鼓了一圈,还是不行 想到fiddler自动侦听了 ...
- javascript进阶高手必备知识
不想当将军的士兵不是好士兵,想当将军那就意味着要学习更多的技能,进阶前端高手必备知识点都有哪些? 你知道作用域.原型.继承.作用域链.闭包等概念吗? 从浏览器多进程到JavaScript单进程你又理解 ...
- 运行Office 2007安装程序提示:"找不到Office.zh-cn\OfficeMUI.xml"(转载)亲测
去网上查结果原来是Office 2007和Visual Studio 2008 Authoring Component组件相冲突,网上说用VS.Net 2008光盘WCU\WebDesignerCor ...
- ionic3样例应用
https://github.com/jujunjun/ionic3-study 该应用包括的样例内容有: 文件上传,上拉更新,下拉加载,弹出层,列表,加载中,栅格,按钮等. php提供的文件上传接口 ...
- Codeforces Round #162 (Div. 1) B. Good Sequences (dp+分解素数)
题目:http://codeforces.com/problemset/problem/264/B 题意:给你一个递增序列,然后找出满足两点要求的最长子序列 第一点是a[i]>a[i-1] 第二 ...