当前位置:网站首页>[leetcode daily question 2021/4/23]368. Maximum divisible subset
[leetcode daily question 2021/4/23]368. Maximum divisible subset
2022-07-26 10:39:00 【LittleSeedling】
368. The largest divisible subset
The title comes from leetcode, Solutions and ideas represent only personal views . Portal .
difficulty : secondary
Time :-
tag: Sort 、 Dynamic programming
subject
Here you are No repetition A set of positive integers nums , Please find out and return the largest subset of them answer , Every element in a subset is a pair of (answer[i], answer[j]) All should be satisfied :
answer[i] % answer[j] == 0 , or
answer[j] % answer[i] == 0
If there are multiple subsets of efficient solutions , Go back to any one of them .
Example 1:
Input :nums = [1,2,3]
Output :[1,2]
explain :[1,3] It will also be seen as the right answer .
Example 2:
Input :nums = [1,2,4,8]
Output :[1,2,4,8]
Tips :
1 <= nums.length <= 1000
1 <= nums[i] <= 2 * 109
nums All integers in Different from each other
Ideas
Dynamic programming
The first point :a % b == 0 or b % a == 0 Can be accepted , So if a > ba % b May be 0, but b % a It must not be for 0
that , We will array 【 Sort 】 after , Just judge a( A larger number ) Divide or not b( Smaller numbers ) that will do .
Second point :
if a % b == 0 && b % c==0 => a % c == 0
however
a % c == 0&&b % c == 0, Can't geta % b == 0
in other words ,% Yes 【 One way transitivity 】
In fact, this question is ok transformation It has become a topic we were very familiar with before 【 Longest ascending subsequence 】.
Why? ? It's not hard to find that
>Also have 【 One way transitivity 】
The third point :
This question is not to ask 【 The length of the ascending subsequence 】, But for 【 Ascending subsequence 】. Then we only need an extra path Array , Record the position of the last smaller element of an element ( What is the previous element of the current element )( Or say , Of this element dp Where did the state transfer from ).
Code
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
vector<int> ans;
int n = nums.size();
vector<int> dp(n,1);
// The path of the previous
vector<int> path(n,-1);
// Sort
sort(nums.begin(),nums.end());
// The endpoint pointer of the largest subset
int p = 0;
// Find the value of the largest subset
int maxAns = 0;
/* * If a%b == 0 ,b%c == 0 , that a%c == 0 ( One way transitivity ) * The idea is similar to 【 Longest ascending subsequence 】 */
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
if(nums[i] % nums[j] == 0){
if(dp[i] <= dp[j]){
dp[i] = dp[j]+1;
path[i] = j;
}
}
if(maxAns < dp[i]){
maxAns = dp[i];
p = i;
}
}
}
while(p != -1){
ans.push_back(nums[p]);
p = path[p];
}
return ans;
}
};
Algorithm complexity
Time complexity : O(n²). The sorting time is O(nlogn). Calculation dp The complexity of array elements is O(n²).
Spatial complexity : O(n).dp The array needs O(n),path The array needs O(n).

边栏推荐
猜你喜欢
随机推荐
oracle 启动不了 tnslistener服务启动不了
el-table实现可编辑表格
一些你不知道的 web API
[leetcode每日一题2021/4/23]368. 最大整除子集
Problems encountered in QRcode QR code (C language)
第4期:大学生提前职业技能准备之一
卸载魅族应用商店
Application of.Net open source framework in industrial production
[Halcon vision] threshold segmentation
Inheritance method of simplified constructor (I) - combined inheritance
第5期:大学生入职必备技能之二
[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数
Redis特殊数据类型使用场景
MLX90640 红外热成像仪测温传感器模块开发笔记(六)红外图像伪彩色编码
第7期:内卷和躺平,你怎么选
Analysis of the transaction problem of chained method call
并行、并发及对于高并发优化的几个方向
.NET操作Redis Set无序集合
剑指Offer(二十):包含min函数的栈
剑指Offer(四十九):把字符串转换成整数





![[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】](/img/13/c6cb176d7065035f60d55ad20ed1bf.png)


![[machine learning notes] [face recognition] deeplearning ai course4 4th week programming](/img/7e/9c0e88097b90c0c24ebf86f090805b.png)
