Very nice DP problem. The key fact of a mutual-divisible subset: if a new number n, is divisible with the largest number m within a mutual-divisible set s, s U {n} is also a mutal-divisible subset.

class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
vector<int> ret;
int n = nums.size();
if(n < ) return nums; sort(nums.begin(), nums.end()); typedef pair<int, int> Rec; // cnt - last inx // init
vector<Rec> dp(n);
for(int i = ; i < n; i ++)
{
dp[i] = {, -};
} //
int max_cnt = ;
int max_inx = ;
for(int i = ; i < n; i ++)
for(int j = i - ; j >= max(,max_cnt - ); j --)
{
if(nums[i] % nums[j] == )
{
int ncnt = dp[j].first + ;
if(ncnt > dp[i].first)
{
dp[i].first = ncnt;
dp[i].second= j;
}
} if(dp[i].first > max_cnt)
{
max_cnt = dp[i].first;
max_inx = i;
}
} // Recover the numbers
while(max_inx >= )
{
ret.push_back(nums[max_inx]);
max_inx = dp[max_inx].second;
}
reverse(ret.begin(), ret.end());
return ret;
}
};

LeetCode "Largest Divisible Subset" !的更多相关文章

  1. [LeetCode] Largest Divisible Subset 最大可整除的子集合

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  2. 【LeetCode】368. Largest Divisible Subset 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...

  3. Leetcode 368. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  4. 【leetcode】368. Largest Divisible Subset

    题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...

  5. Largest Divisible Subset -- LeetCode

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  6. [Swift]LeetCode368. 最大整除子集 | Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  7. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  8. 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  9. 368 Largest Divisible Subset 最大整除子集

    给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0.如果有多个目标子集,返回其中任何一个均 ...

随机推荐

  1. 什么是web service

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...

  2. 数据结构《20》----Immutable stack

    有趣的函数式数据结构<一>----不可变栈 什么是不可变?往栈中插入一个元素,原来的栈保持不变,返回一个新的栈(已插入新的元素). push, pop,getMax 等操作都要求在 常数时 ...

  3. 使用spring @Scheduled注解执行定时任务、

    http://blog.csdn.net/sd4000784/article/details/7745947,留下来备用.

  4. eclipse中一些设置&配置项

    在学习过程中出现的一些问题,以备忘: 1.eclipse中手动生成项目目录结构                 按照这个项目结构建立文件夹,build path -configure build pa ...

  5. anjularjs简介

    1 什么时候该用AngularJS AngularJs(后面就简称ng了)是一个用于设计动态web应用的结构框架.首先,它是一个框架,不是类库,是像EXT一样提供一整套方案用于设计web应用.它不仅仅 ...

  6. JSON入门

    一.简介     1.描述         1)JavaScript 对象表示法(JavaScript Object Notation) 2)存储和交换文本信息的语法.类似 XML 3)比 XML 更 ...

  7. MyBatis笔记

    Mybatis:将java对象映射成SQL语句,再将结果转化为java对象,解决了java对象和sql拼接.结果集的问题,又可以自己写sql语句 总体结构: 根据JDBC规范建立与数据库的连接 通过反 ...

  8. POSTGRES与JDBC对照

    POSTGRES与JDBC对照 未经验证,仅供参考.

  9. gulp学习笔记

    第一步:安装Node 首先,gulp 是基于 Nodejs 的自动任务运行器,所以安装gulp之前,最基本也最重要的是,我们需要搭建node环境.访问http://nodejs.org,下载并安装No ...

  10. input 边框颜色

    border 的属性 有三个 border:5px solid red; 如果上述值缺少一个没有关系,例如border:#FF0000;是允许的. 分别对应:border-width, border- ...