Skip to content

Commit 7080cd0

Browse files
committed
+ problem 910
1 parent 65830da commit 7080cd0

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 910. Smallest Range II
2+
You are given an integer array `nums` and an integer `k`.
3+
4+
For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`.
5+
6+
The **score** of `nums` is the difference between the maximum and minimum elements in `nums`.
7+
8+
Return *the minimum **score** of* `nums` *after changing the values at each index*.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> nums = [1], k = 0
13+
<strong>Output:</strong> 0
14+
<strong>Explanation:</strong> The score is max(nums) - min(nums) = 1 - 1 = 0.
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> nums = [0,10], k = 2
20+
<strong>Output:</strong> 6
21+
<strong>Explanation:</strong> Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.
22+
</pre>
23+
24+
#### Example 3:
25+
<pre>
26+
<strong>Input:</strong> nums = [1,3,6], k = 3
27+
<strong>Output:</strong> 3
28+
<strong>Explanation:</strong> Change nums to be [4, 6, 3]. The score is max(nums) - min(nums) = 6 - 3 = 3.
29+
</pre>
30+
31+
#### Constraints:
32+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
33+
* <code>0 <= nums[i] <= 10<sup>4</sup></code>
34+
* <code>0 <= k <= 10<sup>4</sup></code>
35+
36+
## Solutions (Rust)
37+
38+
### 1. Solution
39+
```Rust
40+
impl Solution {
41+
pub fn smallest_range_ii(nums: Vec<i32>, k: i32) -> i32 {
42+
let n = nums.len();
43+
let mut nums = nums.into_iter().map(|x| x + k).collect::<Vec<_>>();
44+
let mut ret = 0;
45+
46+
nums.sort_unstable();
47+
48+
if nums[n - 1] - 2 * k < nums[0] {
49+
ret = nums[n - 1] - nums[0];
50+
} else {
51+
for i in 1..nums.len() {
52+
if nums[i] - 2 * k >= nums[0] {
53+
ret = nums[i - 1].max(nums[n - 1] - 2 * k) - nums[0];
54+
break;
55+
}
56+
}
57+
}
58+
59+
for i in 1..nums.len() {
60+
if nums[i] - 2 * k > nums[0] {
61+
break;
62+
}
63+
64+
ret = ret.min(nums[i - 1].max(nums[n - 1] - 2 * k) - nums[i] + 2 * k);
65+
}
66+
67+
ret
68+
}
69+
}
70+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 910. 最小差值 II
2+
给你一个整数数组 `nums`,和一个整数 `k`
3+
4+
对于每个下标 `i``0 <= i < nums.length`),将 `nums[i]` 变成 `nums[i] + k``nums[i] - k`
5+
6+
`nums`**分数**`nums` 中最大元素和最小元素的差值。
7+
8+
在更改每个下标对应的值之后,返回 `nums` 的最小 **分数**
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> nums = [1], k = 0
13+
<strong>输出:</strong> 0
14+
<strong>解释:</strong> 分数 = max(nums) - min(nums) = 1 - 1 = 0 。
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> nums = [0,10], k = 2
20+
<strong>输出:</strong> 6
21+
<strong>解释:</strong> 将数组变为 [2, 8] 。分数 = max(nums) - min(nums) = 8 - 2 = 6 。
22+
</pre>
23+
24+
#### 示例 3:
25+
<pre>
26+
<strong>输入:</strong> nums = [1,3,6], k = 3
27+
<strong>输出:</strong> 3
28+
<strong>解释:</strong> 将数组变为 [4, 6, 3] 。分数 = max(nums) - min(nums) = 6 - 3 = 3 。
29+
</pre>
30+
31+
#### 提示:
32+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
33+
* <code>0 <= nums[i] <= 10<sup>4</sup></code>
34+
* <code>0 <= k <= 10<sup>4</sup></code>
35+
36+
## 题解 (Rust)
37+
38+
### 1. 题解
39+
```Rust
40+
impl Solution {
41+
pub fn smallest_range_ii(nums: Vec<i32>, k: i32) -> i32 {
42+
let n = nums.len();
43+
let mut nums = nums.into_iter().map(|x| x + k).collect::<Vec<_>>();
44+
let mut ret = 0;
45+
46+
nums.sort_unstable();
47+
48+
if nums[n - 1] - 2 * k < nums[0] {
49+
ret = nums[n - 1] - nums[0];
50+
} else {
51+
for i in 1..nums.len() {
52+
if nums[i] - 2 * k >= nums[0] {
53+
ret = nums[i - 1].max(nums[n - 1] - 2 * k) - nums[0];
54+
break;
55+
}
56+
}
57+
}
58+
59+
for i in 1..nums.len() {
60+
if nums[i] - 2 * k > nums[0] {
61+
break;
62+
}
63+
64+
ret = ret.min(nums[i - 1].max(nums[n - 1] - 2 * k) - nums[i] + 2 * k);
65+
}
66+
67+
ret
68+
}
69+
}
70+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn smallest_range_ii(nums: Vec<i32>, k: i32) -> i32 {
3+
let n = nums.len();
4+
let mut nums = nums.into_iter().map(|x| x + k).collect::<Vec<_>>();
5+
let mut ret = 0;
6+
7+
nums.sort_unstable();
8+
9+
if nums[n - 1] - 2 * k < nums[0] {
10+
ret = nums[n - 1] - nums[0];
11+
} else {
12+
for i in 1..nums.len() {
13+
if nums[i] - 2 * k >= nums[0] {
14+
ret = nums[i - 1].max(nums[n - 1] - 2 * k) - nums[0];
15+
break;
16+
}
17+
}
18+
}
19+
20+
for i in 1..nums.len() {
21+
if nums[i] - 2 * k > nums[0] {
22+
break;
23+
}
24+
25+
ret = ret.min(nums[i - 1].max(nums[n - 1] - 2 * k) - nums[i] + 2 * k);
26+
}
27+
28+
ret
29+
}
30+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@
569569
[907][907l] |[Sum of Subarray Minimums][907] |![rs]
570570
[908][908l] |[Smallest Range I][908] |![rs]
571571
[909][909l] |[Snakes and Ladders][909] |![rs]
572+
[910][910l] |[Smallest Range II][910] |![rs]
572573
[911][911l] |[Online Election][911] |![rs]
573574
[912][912l] |[Sort an Array][912] |![rs]
574575
[914][914l] |[X of a Kind in a Deck of Cards][914] |![rs]
@@ -2034,6 +2035,7 @@
20342035
[907]:Problemset/0907-Sum%20of%20Subarray%20Minimums/README.md#907-sum-of-subarray-minimums
20352036
[908]:Problemset/0908-Smallest%20Range%20I/README.md#908-smallest-range-i
20362037
[909]:Problemset/0909-Snakes%20and%20Ladders/README.md#909-snakes-and-ladders
2038+
[910]:Problemset/0910-Smallest%20Range%20II/README.md#910-smallest-range-ii
20372039
[911]:Problemset/0911-Online%20Election/README.md#911-online-election
20382040
[912]:Problemset/0912-Sort%20an%20Array/README.md#912-sort-an-array
20392041
[914]:Problemset/0914-X%20of%20a%20Kind%20in%20a%20Deck%20of%20Cards/README.md#914-x-of-a-kind-in-a-deck-of-cards
@@ -3498,6 +3500,7 @@
34983500
[907l]:https://leetcode.com/problems/sum-of-subarray-minimums/
34993501
[908l]:https://leetcode.com/problems/smallest-range-i/
35003502
[909l]:https://leetcode.com/problems/snakes-and-ladders/
3503+
[910l]:https://leetcode.com/problems/smallest-range-ii/
35013504
[911l]:https://leetcode.com/problems/online-election/
35023505
[912l]:https://leetcode.com/problems/sort-an-array/
35033506
[914l]:https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@
569569
[907][907l] |[子数组的最小值之和][907] |![rs]
570570
[908][908l] |[最小差值 I][908] |![rs]
571571
[909][909l] |[蛇梯棋][909] |![rs]
572+
[910][910l] |[最小差值 II][910] |![rs]
572573
[911][911l] |[在线选举][911] |![rs]
573574
[912][912l] |[排序数组][912] |![rs]
574575
[914][914l] |[卡牌分组][914] |![rs]
@@ -2034,6 +2035,7 @@
20342035
[907]:Problemset/0907-Sum%20of%20Subarray%20Minimums/README_CN.md#907-子数组的最小值之和
20352036
[908]:Problemset/0908-Smallest%20Range%20I/README_CN.md#908-最小差值-i
20362037
[909]:Problemset/0909-Snakes%20and%20Ladders/README_CN.md#909-蛇梯棋
2038+
[910]:Problemset/0910-Smallest%20Range%20II/README_CN.md#910-最小差值-ii
20372039
[911]:Problemset/0911-Online%20Election/README_CN.md#911-在线选举
20382040
[912]:Problemset/0912-Sort%20an%20Array/README_CN.md#912-排序数组
20392041
[914]:Problemset/0914-X%20of%20a%20Kind%20in%20a%20Deck%20of%20Cards/README_CN.md#914-卡牌分组
@@ -3498,6 +3500,7 @@
34983500
[907l]:https://leetcode.cn/problems/sum-of-subarray-minimums/
34993501
[908l]:https://leetcode.cn/problems/smallest-range-i/
35003502
[909l]:https://leetcode.cn/problems/snakes-and-ladders/
3503+
[910l]:https://leetcode.cn/problems/smallest-range-ii/
35013504
[911l]:https://leetcode.cn/problems/online-election/
35023505
[912l]:https://leetcode.cn/problems/sort-an-array/
35033506
[914l]:https://leetcode.cn/problems/x-of-a-kind-in-a-deck-of-cards/

0 commit comments

Comments
 (0)