368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0.
If there are multiple solutions, return any subset is fine.
Example 1:
nums: [1,2,3] Result: [1,2] (of course, [1,3] will also be ok)
Example 2:
nums: [1,2,4,8] Result: [1,2,4,8]
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
int n = nums.size(), i, j, k = ;
if(n < )
return nums;
sort(nums.begin(), nums.end());
vector<int> par(n), dp(n, );
for(i = ; i < n; i++)
par[i] = i;
for(i = ; i < n; i++)
{
for(j = ; j < i; j++)
{
if(nums[i] % nums[j])
continue;
if(dp[j]+ > dp[i])
{
dp[i] = dp[j]+;
par[i] = j;
if(dp[i] > dp[k])
k = i;
}
}
}
vector<int> ans;
while(par[k] != k)
{
ans.push_back(nums[k]);
k = par[k];
}
ans.push_back(nums[k]);
return ans;
}
};
368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除的更多相关文章
- c语言题目:找出一个二维数组的“鞍点”,即该位置上的元素在该行上最大,在该列上最小。也可能没有鞍点
//题目:找出一个二维数组的“鞍点”,即该位置上的元素在该行上最大,在该列上最小.也可能没有鞍点. // #include "stdio.h" #include <stdli ...
- C语言程序,找出一个二维数组的鞍点。
什么是鞍点????? 鞍点就是在一个二维数组中,某一个数在该行中最大,然而其在该列中又是最小的数,这样的数称为鞍点. 昨天突然在书上看到这样的一道题,就自己尝试着写了一个找出一个二维数组中的鞍点. 好 ...
- 【LeetCode】368. Largest Divisible Subset 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- 368. Largest Divisible Subset
class Solution { public: vector<int> largestDivisibleSubset(vector<int>& nums) { vec ...
- 368 Largest Divisible Subset 最大整除子集
给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0.如果有多个目标子集,返回其中任何一个均 ...
- 【leetcode】368. Largest Divisible Subset
题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...
- Leetcode 368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- C语言复习---找出一个二维数组的鞍点
前提: 求任意的一个m×n矩阵的鞍点——鞍点是指该位置上的元素在该行上为最大.在该列上为最小, 矩阵中可能没有鞍点,但最多只有一个鞍点. m.n(<=m<=.<=n<=)及矩阵 ...
- 找出一个二维数组中的"鞍点",即该位置上的元素在该行中最大,在该列中最小(也可能没有"鞍点"),打印有关信息.(提示:注意特殊情况:没鞍点或多个鞍点)
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { ][] = {}; ;i < ...
随机推荐
- 【转载】如何系统地自学 Python?
原文:如何系统地自学 Python? 作者:彭猫 本文由 知乎 彭猫 授权发布,版权所有归作者,转载请联系作者! 是否非常想学好 Python,一方面被琐事纠缠,一直没能动手,另一方面,担心学习成本太 ...
- Change An Item Property Using Set_Item_Property In Oracle Forms
Set_Item_Property is used to change an object's settings at run time. Note that in some cases you ca ...
- Javascript this指针
Javascript是一门基于对象的动态语言,也就是说,所有东西都是对象,一个很典型的例子就是函数也被视为普通的对象. 前言 Javascript是一门基于对象的动态语言,也就是说,所有东西都是对 ...
- Linux Shell Bash 带有特殊含义的退出码
linux在执行完一条命令后,使用 echo $? 会显示上一条命令是否执行成功,相关状态码如下 0为成功 表格 D-1. "保留的"退出码 退出码的值 含义 例子 注释 1 通用 ...
- Tomcat6.0的Thisisverylikelytocreateamemoryleak异常
從Apache Tomcat 5.5升級到6.0,通常不用太大的修改,原有的Web Application就能繼續運作.不過在server.xml中設定MySQL Datasource,卻出現一串惱人 ...
- python 标准库
https://www.zhihu.com/question/24590883 https://www.zhihu.com/question/20501628 http://blog.csdn.net ...
- HTML5Canvas标签
- The specified child already has a parent错误
10-05 23:39:48.187: E/AndroidRuntime(12854): Caused by: java.lang.IllegalStateException: The specifi ...
- Unable to execute dex: Multiple dex files define Lcom/gl
[2015-04-16 17:42:04 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/gl/softphon ...
- iphone SE 自带视频播放器要求的视频格式转换参数