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.如果有多个目标子集,返回其中任何一个均 ...
随机推荐
- c# 禁用 webBrowser 声音(转)
const int FEATURE_DISABLE_NAVIGATION_SOUNDS = 21; const int SET_FEATURE_ON_PROCESS = 0x00000002; [Dl ...
- angular 滚动
AngularJs $anchorScroll.$controller.$document $anchorScroll 根据HTML5的规则,当调用这个函数时,它检查当前的url的hash值并且滚动到 ...
- SE Homework 1 —An Error Impressed Me
在对两个对象进行比较.判断是否相等时,直接用 == 放在两个对象中间,例如下面的代码: Infor i1 = new Infor(111,"AA"); Infor i2 = new ...
- web设计中那些因素可能影响网站后期优化
web设计中那些因素可能影响网站后期优化. 1.网站代码的简洁实用性.网站源文件html代码.js代码.css代码等应尽可能的压缩处理.能用jquery-min.js的最好不要用jquery.js:c ...
- [转帖]零投入用panabit享受万元流控设备——搭建篇
原帖地址:http://net.it168.com/a2009/0505/274/000000274918.shtml 你想合理高效的管理内网流量吗?你想针对各个非法网络应用与服务进行合理限制吗?你是 ...
- MTP in Android详解
MTP in Android详解 最近好长一段时间没有做笔记了,今天主要学习一下MTP相关的知识. MTP的全称是Media Transfer Protocol(媒体传输协议),它是微软公司提出的一套 ...
- 遗传算法,实数编码的交叉操作之SBX(模拟二进制交叉)
本文主要介绍遗传算法(实数编码)的交叉操作中的SBX,模拟二进制交叉. 首先,给出个人用python2.7实现的代码,具体模块已上传到: https://github.com/guojun007/sb ...
- 托管到github上的网页图片在百度浏览器中显示不全
这几天做了个较完整的网页放到github上,上传后看网页效果. 在Firefox浏览器中,显示正常. 在百度浏览器中,空了一大块位置(图片位置),偏偏只空了这一块,其它地方的图片都好好的. 点击f12 ...
- 利用jQuery和CSS实现环形进度条
实现原理 原理非常的简单,在这个方案中,最主要使用了CSS3的transform中的rotate和CSS3的clip两个属性.用他们来实现半圆和旋转效果. 半环的实现 先来看其结构. HTML < ...
- tomcat取带有中文的参数乱码的解决办法
1. 对于post参数,可以用filter来处理,在dofilter之前,加入以下代码: request.setCharacterEncoding("UTF-8"); 2. 对于g ...