当前位置:网站首页>[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 > b
a % 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创建索引
- mysql20210906
- putty的使用教程
- 数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察
- Issue 7: how do you choose between curling up and lying flat
- L2-005 set similarity (intersection of vector and set)
- [notes on machine learning] [building a cyclic neural network and its application] deeplearning ai course5 1st week programming(keras)
- [leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间
- mysql 进不去了怎么办
- C语言计算日期间隔天数
猜你喜欢
【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
第6期:大学生应该选择哪种主流编程语言
Write to esp8266 burning brush firmware
多目标优化系列1---NSGA2的非支配排序函数的讲解
数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察
centos8(liunx)部署WTM(ASP.NET 5)使用pgsql
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
[Halcon vision] morphological expansion
文案秘籍七步曲至----文献团队协作管理
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间
随机推荐
STM32 Alibaba cloud mqtt esp8266 at command
Application of.Net open source framework in industrial production
【机器学习小记】【风格迁移】deeplearning.ai course4 4th week programming(tensorflow2)
[leetcode每日一题2021/2/14]765. 情侣牵手
Oracle创建索引
.net5wtm (asp.net core) PgSQL unpacking operation
L2-005 set similarity (intersection of vector and set)
11 在 operator= 中处理“自我赋值”
Write to esp8266 burning brush firmware
L2-005 集合相似度(vector、set求并交集)
Parallelism, concurrency and several directions for high concurrency optimization
RT-Thread 学习笔记(八)---开启基于SPI Flash的elmfat文件系统(下)
.net operation redis string string
剑指Offer(五十二):正则化表达式
同步方法中不使用asyncTask<T> 修饰和await获取异步返回值(同步方法中调用异步方法)
剑指Offer(四十三):左旋转字符串
使用Geoprocessor 工具
Issue 5: the second essential skill for College Students
鹏哥C语言第六节课
剑指Offer(五):用两个栈实现队列