LeetCode: 368. Largest Divisible Subset
来源:互联网 发布:网络计算机培训 编辑:程序博客网 时间:2024/05/01 06:42
题目链接: https://leetcode.com/problems/largest-divisible-subset/#/description
这是一道动态规划题,重点是写出他的状态转移方程,这道题状态转移方程是: dp[i] = dp[j] +1 ( 当nums[j] % nums[i] == 0)。 所以可以得出解题代码:
class Solution {public: vector<int> largestDivisibleSubset(vector<int>& nums) { sort(nums.begin(),nums.end()); vector<int> result; int length = nums.size(); int max=0,max_i=0; vector<int> parent(length,0), dp(length,0); for(int i =length-1; i>=0; --i){ for(int j = i; j<length; ++j){ if(nums[j] % nums[i] == 0 && dp[i] <1+dp[j]) { dp[i] = dp[j] + 1; parent[i] = j; if(max < dp[i]) {max= dp[i]; max_i = i;} } } } for(int i =0; i< max; ++i){ result.push_back(nums[max_i]); max_i = parent[max_i]; } return result; }};
阅读全文
0 0
- [leetcode] 368. Largest Divisible Subset
- Leetcode 368. Largest Divisible Subset
- LeetCode 368. Largest Divisible Subset
- leetcode 368. Largest Divisible Subset
- [leetcode] 368. Largest Divisible Subset
- leetcode.368. Largest Divisible Subset
- [Leetcode]368. Largest Divisible Subset
- [leetcode] 368. Largest Divisible Subset
- leetcode 368. Largest Divisible Subset
- [leetcode] 368. Largest Divisible Subset
- [leetcode] 368. Largest Divisible Subset
- Leetcode: 368. Largest Divisible Subset
- *Leetcode 368. Largest Divisible Subset
- Leetcode-368. Largest Divisible Subset
- Leetcode 368. Largest Divisible Subset
- Leetcode 368. Largest Divisible Subset
- LeetCode 368. Largest Divisible Subset
- [LeetCode]368. Largest Divisible Subset
- ZOJ-1259 输出所有的出站情况
- GitHub:pull最新内容
- 欢迎使用CSDN-markdown编辑器
- python 关于decorator
- java Integer与int的装箱(缓存)与拆箱
- LeetCode: 368. Largest Divisible Subset
- Controller控制器
- POJ 2299Ultra-QuickSort
- 我的学习之路_第九章_File类,递归
- ZOJ--1078:Palindrom Numbers
- Mysql主从复制原理解析和技术难点解决
- nginx 学习笔记(三)基本数据结构
- 考研英语
- [置顶] Android APK反编译就这么简单 详解(附图)