Skip to content

Commit 93693e3

Browse files
committed
✨feat: Add 908
1 parent b9f5da5 commit 93693e3

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

Index/脑筋急转弯.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| [335. 路径交叉](https://leetcode-cn.com/problems/self-crossing/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/self-crossing/solution/gong-shui-san-xie-fen-qing-kuang-tao-lun-zdrb/) | 困难 | 🤩🤩🤩🤩 |
44
| [419. 甲板上的战舰](https://leetcode-cn.com/problems/battleships-in-a-board/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/battleships-in-a-board/solution/gong-shui-san-xie-ji-chong-sao-miao-xian-trmc/) | 中等 | 🤩🤩🤩🤩 |
55
| [423. 从英文中重建数字](https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/solution/gong-shui-san-xie-nao-jin-ji-zhuan-wan-m-vg7a/) | 中等 | 🤩🤩🤩🤩 |
6+
| [908. 最小差值 I](https://leetcode.cn/problems/smallest-range-i/) | [LeetCode 题解链接](https://leetcode.cn/problems/smallest-range-i/solution/by-ac_oier-7fh0/) | 简单 | 🤩🤩🤩🤩 |
67
| [2038. 如果相邻两个颜色均相同则删除当前颜色](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/solution/gong-shui-san-xie-nao-jin-ji-zhuan-wan-y-a8xm/) | 中等 | 🤩🤩🤩🤩🤩 |
78
| [2069. 模拟行走机器人 II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/walking-robot-simulation-ii/solution/by-ac_oier-6zib/) | 中等 | 🤩🤩🤩🤩 |
89

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[908. 最小差值 I](https://leetcode.cn/problems/smallest-range-i/solution/by-ac_oier-7fh0/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「脑筋急转弯」
6+
7+
8+
9+
给你一个整数数组 `nums`,和一个整数 `k`
10+
11+
在一个操作中,您可以选择 $0 <= i < nums.length$ 的任何索引 `i` 。将 $nums[i]$ 改为 $nums[i] + x$ ,其中 $x$ 是一个范围为 $[-k, k]$ 的整数。对于每个索引 `i` ,最多 只能 应用 一次 此操作。
12+
13+
`nums` 的 分数 是 `nums` 中最大和最小元素的差值。 
14+
15+
在对  `nums` 中的每个索引最多应用一次上述操作后,返回 `nums` 的最低 分数 。
16+
17+
示例 1:
18+
```
19+
输入:nums = [1], k = 0
20+
21+
输出:0
22+
23+
解释:分数是 max(nums) - min(nums) = 1 - 1 = 0。
24+
```
25+
示例 2:
26+
```
27+
输入:nums = [0,10], k = 2
28+
29+
输出:6
30+
31+
解释:将 nums 改为 [2,8]。分数是 max(nums) - min(nums) = 8 - 2 = 6。
32+
```
33+
示例 3:
34+
```
35+
输入:nums = [1,3,6], k = 3
36+
37+
输出:0
38+
39+
解释:将 nums 改为 [4,4,4]。分数是 max(nums) - min(nums) = 4 - 4 = 0。
40+
```
41+
42+
提示:
43+
* $1 <= nums.length <= 10^4$
44+
* $0 <= nums[i] <= 10^4$
45+
* $0 <= k <= 10^4$
46+
47+
---
48+
49+
### 脑筋急转弯
50+
51+
今天胃不是很舒服,来晚了。
52+
53+
根据题意,对于任意一个数 $nums[i]$ 而言,其所能变化的范围为 $[nums[i] - k, nums[i] + k]$,我们需要最小化变化后的差值。而当 $k$ 足够大时,我们必然能够将所有数变为同一个值,此时答案为 $0$,而更一般的情况,我们能够缩减的数值距离为 $2 * k$,因此如果原来的最大差值为 $d = \max - \min$,若 $d <= 2 * k$ 时,答案为 $0$,否则答案为 $d - 2 * k$。
54+
55+
代码:
56+
```Java
57+
class Solution {
58+
public int smallestRangeI(int[] nums, int k) {
59+
int max = nums[0], min = nums[0];
60+
for (int i : nums) {
61+
max = Math.max(max, i);
62+
min = Math.min(min, i);
63+
}
64+
return Math.max(0, max - min - 2 * k);
65+
}
66+
}
67+
```
68+
* 时间复杂度:$O(n)$
69+
* 空间复杂度:$O(1)$
70+
71+
---
72+
73+
### 最后
74+
75+
这是我们「刷穿 LeetCode」系列文章的第 `No.908` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
76+
77+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
78+
79+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
80+
81+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
82+

0 commit comments

Comments
 (0)