LeetCode "Largest Divisible Subset" !
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" !的更多相关文章
- [LeetCode] 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 ...
- 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
题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...
- Largest Divisible Subset -- LeetCode
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 ...
- 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 ...
- 368 Largest Divisible Subset 最大整除子集
给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0.如果有多个目标子集,返回其中任何一个均 ...
随机推荐
- What is the difference between the ways to implement inheritance in javascript.
see also : http://www.w3school.com.cn/js/pro_js_inheritance_implementing.asp http://davidshariff.com ...
- PHP时区配置
在PHP安装目录中找到 php.ini-development 复制创建新的副本 找到 :date.timezone = 修改为 date.timezone = PRC 并保存为php.ini PRC ...
- Drools环境搭建(转)
Eclipse3.5安装Drools5.2.0.Final插件 到Drools下载页面(现在是http://www.jboss.org/drools/downloads.html) -下载并解压Dro ...
- fork函数创建新进程过程分析
gdb调试执行流程,首先设置断点b sys_clone,当在shell下输入fork命令后,系统执行至断点,接下来按步执行: 判断是否被跟踪 判断是否被创建为轻量级进程(vfork) 判断父进程是否被 ...
- js 自运行函数作用
var obj = new Object(); function test2() { for (var i=1;i<5;i++) { obj['f'+i] = function() { retu ...
- LayaAir引擎——(七)
LayaAir引擎——人物控制TiledMap地图移动和墙壁检测 所需要的软件: LayaAir IDE 1.0.2版本 TiledMap 所需要的东西: 地图:53 * 32,(48*48) 人物: ...
- C# xpath
XPath最通俗的教程(ZZ) 以下是本人找到的最完整最易懂的XPath教程,不敢私藏,拿出来与大家分享.帮我点旁边的google广告呀. 实例 1基本的XPath语法类似于在一个文件系统中定位文 ...
- bs4 python解析html
使用文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ python的编码问题比较恶心. decode解码encode编码 在文件 ...
- 缓存和sd卡的路径(原)
在需要存储的时候,路径的问题是初学者比较迷惑的,下面是对于getCacheDir().getFilesDir().getExternalFilesDir().getExternalCacheDir() ...
- JUCE 界面库显示中文乱码问题
JUCE 界面库显示中文乱码问题 环境: Windows7 64位 旗舰版 Visual Studio Ultimate 2012 JUCE 4.1 问题描述: 直接使用juce::String存储中 ...