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.如果有多个目标子集,返回其中任何一个均 ...
随机推荐
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- C#中隐藏(new)、方法重写(override)、重载(overload)的区别
转自:http://www.cnblogs.com/glife/archive/2009/12/28/1633947.html 重载.重写和隐藏的定义: 重载:public string ToStri ...
- [转]Linux 的多线程编程的高效开发经验
Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微和隐晦的差别.不注意这些 Linux 上的一些开发陷阱,常常会导致程序问题不穷,死锁不断.本文中我们 ...
- IOS 更改百度地图的定位图片
使用了百度地图的SDK,定位图片一直是蓝色的小圆点,很不喜欢,想换成自定义的图片,在网上搜罗了一大通,找到了解决的方案. 写下如下代码: //定位图层自定义样式参数 BMKLocationViewDi ...
- HTTP协议 (三) 压缩
之前写过一个篇 [HTTP协议详解] ,这次继续介绍HTTP协议中的压缩. 本文会使用Fiddler来查看HTTP request和Response, 如果不熟悉这个工具,可以先参考[Fiddler教 ...
- 带head的gridview
这是github上的一个项目,根据谷歌的那个HeadGridView改的,因为谷歌的那个addHeadView后宽度不能填充屏幕,下面是代码. 来源:https://github.com/liaohu ...
- java考核完的心得
2015年8月13日 星期四 晴 今天上午我们考核了java,就是用Java语言写一个小型的管理系统.考核前的我信心满满,因为我本身是测试专业的,觉得在开发这方面对自己不用要求的那么 ...
- git提交远程仓库命令
在已有的git库中搭建新库,并且将本地的git仓库,上传到远程服务器的git库中,从而开始一个新的项目 首先,在本地新建文件夹abc,进入到abc里面,然后git init.这样就在本地初始化了一个g ...
- Android深度探索HAL与驱动开发 第二章 搭建Android开发环境
通常以应用移植和系统移植的为Andorid系统移植的主要部分.同时为了适应不同平台硬件设备,对源代码进行相应的修改.除了要移植相适应的CPU架构,最重要是移植Linux驱动. 但是由于一些特别的原因, ...
- Tomcat编码配置解疑
环境:tomcat6.0.23 jdk 1.6 相关参考: http://tomcat.apache.org/tomcat-7.0-doc/config/ajp.html http://wiki.ap ...