Skip to content

Commit 5479da0

Browse files
committed
+ problem 368
1 parent e19531a commit 5479da0

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 368. Largest Divisible Subset
2+
Given a set of **distinct** positive integers `nums`, return the largest subset `answer` such that every pair `(answer[i], answer[j])` of elements in this subset satisfies:
3+
4+
* `answer[i] % answer[j] == 0`, or
5+
* `answer[j] % answer[i] == 0`
6+
7+
If there are multiple solutions, return any of them.
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> nums = [1,2,3]
12+
<strong>Output:</strong> [1,2]
13+
<strong>Explanation:</strong> [1,3] is also accepted.
14+
</pre>
15+
16+
#### Example 2:
17+
<pre>
18+
<strong>Input:</strong> nums = [1,2,4,8]
19+
<strong>Output:</strong> [1,2,4,8]
20+
</pre>
21+
22+
#### Constraints:
23+
* `1 <= nums.length <= 1000`
24+
* <code>1 <= nums[i] <= 2 * 10<sup>9</sup></code>
25+
* All the integers in `nums` are **unique**.
26+
27+
## Solutions (Rust)
28+
29+
### 1. Solution
30+
```Rust
31+
impl Solution {
32+
pub fn largest_divisible_subset(nums: Vec<i32>) -> Vec<i32> {
33+
let mut nums = nums;
34+
let mut max_len = vec![1; nums.len()];
35+
let mut prev_index = vec![None; nums.len()];
36+
let mut answer = vec![];
37+
38+
nums.sort_unstable();
39+
40+
for i in 1..nums.len() {
41+
for j in 0..i {
42+
if nums[i] % nums[j] == 0 && max_len[i] < max_len[j] + 1 {
43+
max_len[i] = max_len[j] + 1;
44+
prev_index[i] = Some(j);
45+
}
46+
}
47+
}
48+
49+
let mut curr = (0..nums.len()).max_by_key(|&i| max_len[i]).unwrap();
50+
answer.push(nums[curr]);
51+
52+
while let Some(i) = prev_index[curr] {
53+
curr = i;
54+
answer.push(nums[curr]);
55+
}
56+
57+
answer
58+
}
59+
}
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 368. 最大整除子集
2+
给你一个由 无重复 正整数组成的集合 `nums` ,请你找出并返回其中最大的整除子集 `answer` ,子集中每一元素对 `(answer[i], answer[j])` 都应当满足:
3+
4+
* `answer[i] % answer[j] == 0` ,或
5+
* `answer[j] % answer[i] == 0`
6+
7+
如果存在多个有效解子集,返回其中任何一个均可。
8+
9+
#### 示例 1:
10+
<pre>
11+
<strong>输入:</strong> nums = [1,2,3]
12+
<strong>输出:</strong> [1,2]
13+
<strong>解释:</strong> [1,3] 也会被视为正确答案。
14+
</pre>
15+
16+
#### 示例 2:
17+
<pre>
18+
<strong>输入:</strong> nums = [1,2,4,8]
19+
<strong>输出:</strong> [1,2,4,8]
20+
</pre>
21+
22+
#### Constraints:
23+
* `1 <= nums.length <= 1000`
24+
* <code>1 <= nums[i] <= 2 * 10<sup>9</sup></code>
25+
* `nums` 中的所有整数 **互不相同**
26+
27+
## 题解 (Rust)
28+
29+
### 1. 题解
30+
```Rust
31+
impl Solution {
32+
pub fn largest_divisible_subset(nums: Vec<i32>) -> Vec<i32> {
33+
let mut nums = nums;
34+
let mut max_len = vec![1; nums.len()];
35+
let mut prev_index = vec![None; nums.len()];
36+
let mut answer = vec![];
37+
38+
nums.sort_unstable();
39+
40+
for i in 1..nums.len() {
41+
for j in 0..i {
42+
if nums[i] % nums[j] == 0 && max_len[i] < max_len[j] + 1 {
43+
max_len[i] = max_len[j] + 1;
44+
prev_index[i] = Some(j);
45+
}
46+
}
47+
}
48+
49+
let mut curr = (0..nums.len()).max_by_key(|&i| max_len[i]).unwrap();
50+
answer.push(nums[curr]);
51+
52+
while let Some(i) = prev_index[curr] {
53+
curr = i;
54+
answer.push(nums[curr]);
55+
}
56+
57+
answer
58+
}
59+
}
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
impl Solution {
2+
pub fn largest_divisible_subset(nums: Vec<i32>) -> Vec<i32> {
3+
let mut nums = nums;
4+
let mut max_len = vec![1; nums.len()];
5+
let mut prev_index = vec![None; nums.len()];
6+
let mut answer = vec![];
7+
8+
nums.sort_unstable();
9+
10+
for i in 1..nums.len() {
11+
for j in 0..i {
12+
if nums[i] % nums[j] == 0 && max_len[i] < max_len[j] + 1 {
13+
max_len[i] = max_len[j] + 1;
14+
prev_index[i] = Some(j);
15+
}
16+
}
17+
}
18+
19+
let mut curr = (0..nums.len()).max_by_key(|&i| max_len[i]).unwrap();
20+
answer.push(nums[curr]);
21+
22+
while let Some(i) = prev_index[curr] {
23+
curr = i;
24+
answer.push(nums[curr]);
25+
}
26+
27+
answer
28+
}
29+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
[357][357l] |[Count Numbers with Unique Digits][357] |![rs]
221221
[365][365l] |[Water and Jug Problem][365] |![rb]
222222
[367][367l] |[Valid Perfect Square][367] |![rs]
223+
[368][368l] |[Largest Divisible Subset][368] |![rs]
223224
[371][371l] |[Sum of Two Integers][371] |![rs]
224225
[372][372l] |[Super Pow][372] |![rs]
225226
[374][374l] |[Guess Number Higher or Lower][374] |![py]
@@ -1555,6 +1556,7 @@
15551556
[357]:Problemset/0357-Count%20Numbers%20with%20Unique%20Digits/README.md#357-count-numbers-with-unique-digits
15561557
[365]:Problemset/0365-Water%20and%20Jug%20Problem/README.md#365-water-and-jug-problem
15571558
[367]:Problemset/0367-Valid%20Perfect%20Square/README.md#367-valid-perfect-square
1559+
[368]:Problemset/0368-Largest%20Divisible%20Subset/README.md#368-largest-divisible-subset
15581560
[371]:Problemset/0371-Sum%20of%20Two%20Integers/README.md#371-sum-of-two-integers
15591561
[372]:Problemset/0372-Super%20Pow/README.md#372-super-pow
15601562
[374]:Problemset/0374-Guess%20Number%20Higher%20or%20Lower/README.md#374-guess-number-higher-or-lower
@@ -2889,6 +2891,7 @@
28892891
[357l]:https://leetcode.com/problems/count-numbers-with-unique-digits/
28902892
[365l]:https://leetcode.com/problems/water-and-jug-problem/
28912893
[367l]:https://leetcode.com/problems/valid-perfect-square/
2894+
[368l]:https://leetcode.com/problems/largest-divisible-subset/
28922895
[371l]:https://leetcode.com/problems/sum-of-two-integers/
28932896
[372l]:https://leetcode.com/problems/super-pow/
28942897
[374l]:https://leetcode.com/problems/guess-number-higher-or-lower/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
[357][357l] |[计算各个位数不同的数字个数][357] |![rs]
221221
[365][365l] |[水壶问题][365] |![rb]
222222
[367][367l] |[有效的完全平方数][367] |![rs]
223+
[368][368l] |[最大整除子集][368] |![rs]
223224
[371][371l] |[两整数之和][371] |![rs]
224225
[372][372l] |[超级次方][372] |![rs]
225226
[374][374l] |[猜数字大小][374] |![py]
@@ -1555,6 +1556,7 @@
15551556
[357]:Problemset/0357-Count%20Numbers%20with%20Unique%20Digits/README_CN.md#357-计算各个位数不同的数字个数
15561557
[365]:Problemset/0365-Water%20and%20Jug%20Problem/README_CN.md#365-水壶问题
15571558
[367]:Problemset/0367-Valid%20Perfect%20Square/README_CN.md#367-有效的完全平方数
1559+
[368]:Problemset/0368-Largest%20Divisible%20Subset/README_CN.md#368-最大整除子集
15581560
[371]:Problemset/0371-Sum%20of%20Two%20Integers/README_CN.md#371-两整数之和
15591561
[372]:Problemset/0372-Super%20Pow/README_CN.md#372-超级次方
15601562
[374]:Problemset/0374-Guess%20Number%20Higher%20or%20Lower/README_CN.md#374-猜数字大小
@@ -2889,6 +2891,7 @@
28892891
[357l]:https://leetcode.cn/problems/count-numbers-with-unique-digits/
28902892
[365l]:https://leetcode.cn/problems/water-and-jug-problem/
28912893
[367l]:https://leetcode.cn/problems/valid-perfect-square/
2894+
[368l]:https://leetcode.cn/problems/largest-divisible-subset/
28922895
[371l]:https://leetcode.cn/problems/sum-of-two-integers/
28932896
[372l]:https://leetcode.cn/problems/super-pow/
28942897
[374l]:https://leetcode.cn/problems/guess-number-higher-or-lower/

0 commit comments

Comments
 (0)