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. 2048游戏C语言代码

    如果程序里面有错误,希望大家能够批评指正! #include<stdio.h> #include<stdlib.h> #include<conio.h> #incl ...

  2. 关于eclipse中DDMS中Emulator Control选项卡为灰色不可用

    首先先感谢版主:http://blog.csdn.net/noname666/article/details/51670905#reply 方法一的出处:http://stackoverflow.co ...

  3. IntelliJ IDEA注册码

    1.打开IJ,help选项下的register 2. 选择Activation code 将注册码粘到内容框里,点击ok即可 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QT ...

  4. JXL操作Excel

    jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文 ...

  5. PC端和手机访问调用不同的页面,JS和PHP不同方法

    js方法: var urlqr = location.href;    urlqr = urlqr.replace(/iphoneF_/, "pc")这里写正则替换页面地址 if( ...

  6. HttpContext详解

    HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息. 在处理请求执行链的各个阶段中,会有一个对象在各个对象之间进行传递,也即会保存 请求的上下文信息,这个对象就是Ht ...

  7. SDK截图(三):压缩位图之理论准备篇

    压缩位图我们使用简单的行程长度编码的方法,详情参考<windows程序设计>15章之DIB压缩. 在BITMAPINFOHEADER结构中有两个和位图压缩有关的字段,分别是biCompre ...

  8. pdf生成器

    apose, http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version

  9. 总结C#保留小数位数及百分号处理

    方法一: ); 方法二: Math.Round() 方法三: double dbdata = 0.55555; string str1 = dbdata.ToString("f2" ...

  10. IOS开发网络数据---- AFNetworking的使用

    http网络库是集XML解析,Json解析,网络图片下载,plist解析,数据流请求操作,上传,下载,缓存等网络众多功能于一身的强大的类库.最新版本支持session,xctool单元测试.网络获取数 ...