368:最大整除子集
This commit is contained in:
parent
494ffa0805
commit
3539a0cc78
@ -0,0 +1,118 @@
|
|||||||
|
//给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一元素对 (answer[i], answer[
|
||||||
|
//j]) 都应当满足:
|
||||||
|
//
|
||||||
|
// answer[i] % answer[j] == 0 ,或
|
||||||
|
// answer[j] % answer[i] == 0
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 如果存在多个有效解子集,返回其中任何一个均可。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,2,3]
|
||||||
|
//输出:[1,2]
|
||||||
|
//解释:[1,3] 也会被视为正确答案。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,2,4,8]
|
||||||
|
//输出:[1,2,4,8]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 1000
|
||||||
|
// 1 <= nums[i] <= 2 * 109
|
||||||
|
// nums 中的所有整数 互不相同
|
||||||
|
//
|
||||||
|
// Related Topics 数学 动态规划
|
||||||
|
// 👍 232 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
//368:最大整除子集
|
||||||
|
public class LargestDivisibleSubset {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new LargestDivisibleSubset().new Solution();
|
||||||
|
//1,2
|
||||||
|
// solution.largestDivisibleSubset(new int[]{1, 2, 3});
|
||||||
|
//1,2,4,8
|
||||||
|
solution.largestDivisibleSubset(new int[]{1, 2, 4, 8});
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public List<Integer> largestDivisibleSubset(int[] nums) {
|
||||||
|
// int length = nums.length;
|
||||||
|
// Arrays.sort(nums);
|
||||||
|
//
|
||||||
|
// int[] dp = new int[length];
|
||||||
|
// Arrays.fill(dp, 1);
|
||||||
|
// int maxSize = 1;
|
||||||
|
// int maxVal = dp[0];
|
||||||
|
// for (int i = 1; i < length; i++) {
|
||||||
|
// for (int j = 0; j < i; j++) {
|
||||||
|
// if (nums[i] % nums[j] == 0) {
|
||||||
|
// dp[i] = Math.max(dp[i], dp[j] + 1);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (dp[i] > maxSize) {
|
||||||
|
// maxSize = dp[i];
|
||||||
|
// maxVal = nums[i];
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// List<Integer> res = new ArrayList<Integer>();
|
||||||
|
// if (maxSize == 1) {
|
||||||
|
// res.add(nums[0]);
|
||||||
|
// return res;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// for (int i = length - 1; i >= 0 && maxSize > 0; i--) {
|
||||||
|
// if (dp[i] == maxSize && maxVal % nums[i] == 0) {
|
||||||
|
// res.add(nums[i]);
|
||||||
|
// maxVal = nums[i];
|
||||||
|
// maxSize--;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return res;
|
||||||
|
Arrays.sort(nums);
|
||||||
|
int length = nums.length;
|
||||||
|
List<Integer>[] list = new List[length];
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
list[i] = new ArrayList<>();
|
||||||
|
}
|
||||||
|
int size = 0;
|
||||||
|
int index = 0;
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
for (int j = 0; j < i && nums[j] <= nums[i] / 2; j++) {
|
||||||
|
if (nums[i] % nums[j] == 0 && list[i].size() < list[j].size()) {
|
||||||
|
list[i] = new ArrayList<>();
|
||||||
|
list[i].addAll(list[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list[i].add(nums[i]);
|
||||||
|
if (size < list[i].size()) {
|
||||||
|
size = list[i].size();
|
||||||
|
index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
给你一个由 <strong>无重复</strong> 正整数组成的集合 <code>nums</code> ,请你找出并返回其中最大的整除子集 <code>answer</code> ,子集中每一元素对 <code>(answer[i], answer[j])</code> 都应当满足:
|
||||||
|
<ul>
|
||||||
|
<li><code>answer[i] % answer[j] == 0</code> ,或</li>
|
||||||
|
<li><code>answer[j] % answer[i] == 0</code></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>如果存在多个有效解子集,返回其中任何一个均可。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,2,3]
|
||||||
|
<strong>输出:</strong>[1,2]
|
||||||
|
<strong>解释:</strong>[1,3] 也会被视为正确答案。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,2,4,8]
|
||||||
|
<strong>输出:</strong>[1,2,4,8]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 1000</code></li>
|
||||||
|
<li><code>1 <= nums[i] <= 2 * 10<sup>9</sup></code></li>
|
||||||
|
<li><code>nums</code> 中的所有整数 <strong>互不相同</strong></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数学</li><li>动态规划</li></div></div>\n<div><li>👍 232</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user