From 93693e33b299c72a3d3e77d3f5153ecf78becb1a Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Sat, 30 Apr 2022 10:25:50 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=20908?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\346\200\245\350\275\254\345\274\257.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 "LeetCode/901-910/908. \346\234\200\345\260\217\345\267\256\345\200\274 I\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\350\204\221\347\255\213\346\200\245\350\275\254\345\274\257.md" "b/Index/\350\204\221\347\255\213\346\200\245\350\275\254\345\274\257.md" index 45309b77..57a6133e 100644 --- "a/Index/\350\204\221\347\255\213\346\200\245\350\275\254\345\274\257.md" +++ "b/Index/\350\204\221\347\255\213\346\200\245\350\275\254\345\274\257.md" @@ -3,6 +3,7 @@ | [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/) | 困难 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | +| [908. 最小差值 I](https://leetcode.cn/problems/smallest-range-i/) | [LeetCode 题解链接](https://leetcode.cn/problems/smallest-range-i/solution/by-ac_oier-7fh0/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/901-910/908. \346\234\200\345\260\217\345\267\256\345\200\274 I\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/901-910/908. \346\234\200\345\260\217\345\267\256\345\200\274 I\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..647e1f7f --- /dev/null +++ "b/LeetCode/901-910/908. \346\234\200\345\260\217\345\267\256\345\200\274 I\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,82 @@ +### 题目描述 + +这是 LeetCode 上的 **[908. 最小差值 I](https://leetcode.cn/problems/smallest-range-i/solution/by-ac_oier-7fh0/)** ,难度为 **简单**。 + +Tag : 「模拟」、「脑筋急转弯」 + + + +给你一个整数数组 `nums`,和一个整数 `k` 。 + +在一个操作中,您可以选择 $0 <= i < nums.length$ 的任何索引 `i` 。将 $nums[i]$ 改为 $nums[i] + x$ ,其中 $x$ 是一个范围为 $[-k, k]$ 的整数。对于每个索引 `i` ,最多 只能 应用 一次 此操作。 + +`nums` 的 分数 是 `nums` 中最大和最小元素的差值。  + +在对  `nums` 中的每个索引最多应用一次上述操作后,返回 `nums` 的最低 分数 。 + +示例 1: +``` +输入:nums = [1], k = 0 + +输出:0 + +解释:分数是 max(nums) - min(nums) = 1 - 1 = 0。 +``` +示例 2: +``` +输入:nums = [0,10], k = 2 + +输出:6 + +解释:将 nums 改为 [2,8]。分数是 max(nums) - min(nums) = 8 - 2 = 6。 +``` +示例 3: +``` +输入:nums = [1,3,6], k = 3 + +输出:0 + +解释:将 nums 改为 [4,4,4]。分数是 max(nums) - min(nums) = 4 - 4 = 0。 +``` + +提示: +* $1 <= nums.length <= 10^4$ +* $0 <= nums[i] <= 10^4$ +* $0 <= k <= 10^4$ + +--- + +### 脑筋急转弯 + +今天胃不是很舒服,来晚了。 + +根据题意,对于任意一个数 $nums[i]$ 而言,其所能变化的范围为 $[nums[i] - k, nums[i] + k]$,我们需要最小化变化后的差值。而当 $k$ 足够大时,我们必然能够将所有数变为同一个值,此时答案为 $0$,而更一般的情况,我们能够缩减的数值距离为 $2 * k$,因此如果原来的最大差值为 $d = \max - \min$,若 $d <= 2 * k$ 时,答案为 $0$,否则答案为 $d - 2 * k$。 + +代码: +```Java +class Solution { + public int smallestRangeI(int[] nums, int k) { + int max = nums[0], min = nums[0]; + for (int i : nums) { + max = Math.max(max, i); + min = Math.min(min, i); + } + return Math.max(0, max - min - 2 * k); + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.908` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +