Given an array `A` of integers, for each integer `A[i]` we need to choose either `x = -K` or `x = K`, and add `x` to `A[i] (only once)`.

After this process, we have some array B.

Return the smallest possible difference between the maximum value of B and the minimum value of B.

Example 1:

Input: A = [1], K = 0
Output: 0
Explanation: B = [1]

Example 2:

Input: A = [0,10], K = 2
Output: 6 Explanation: B = [2,8]

Example 3:

Input: A = [1,3,6], K = 3
Output: 3
Explanation: B = [4,6,3]

Note:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 10000
  3. 0 <= K <= 10000

这道题是之前那道 [Smallest Range I](https://www.cnblogs.com/grandyang/p/11318240.html) 的拓展,那道题说的是每个数字可以加上 [-K, K] 范围内的任意一个数字,这道题说是每个数字必须加上K或者 —K,都是问新数组的最大值最小值之间的差值最小是多少。这两个条件有显著的区别,对于前一道题来说,非常的 flexible,因为可以加上 [-K, K] 范围内的任意一个数字,就是说也可以加上0,从而数字保持不变,那么只需要对原数组的最大值最小值进行修改即可,而这道题所有数字都强制要进行修改,则只考虑最大值最小值显然是不对的。来想想为什么不能直接对最小值加K,最大值减K,然后算差值。来看题目中的例子3,A = [1,3,6], K = 3,对最小值加K,得到4,对最大值减K,得到3,相减得到 -1,但实际上是不对的,因为中间的3也要进行加减操作,只能变成0或6,这样相当于改变了新数组的最大值最小值,最终算下来的结果应该是3。博主其实也尝试了暴力搜索法,即将原数组中的每个数字进行加K和减K,把得到的两个新数字放到一个新数组中,最后遍历新数组,找出最大值最小值并做差,结果超时了 Time Limit Exceeded!即便这只是一道 Medium 的题目,仍然不许我们用如此 Naive 的方法。只能另辟蹊径了。

如果不考虑数字修改,那么原数组的最大值最小值之间的差值就是所求,这里可以当作结果 res 的初始值。由于每个数字都需要加K或者减K,为了使得新数组的最大值最小值的差值最小,应该尽量使原数组中的较小的数字加K,较大的数字减K,所以最好是先给原数组排个序,然后在数组的某个位置i为界限,将原数组分为两段,前面所有的数字都加K,后面所有的数字都减K。则前半段 [0, i] 中的最大值是 A[i]+K,最小值是 A[0]+K,后半段 [i+1, n-1] 范围内的最大值是 A[n-1]-K,最小值是 A[i+1]-K,所以整个数组的最大值是 A[i]+K 和 A[n-1]-K 中的较大值,最小值是 A[0]+K 和 A[i+1]-K 中的较小值,二者做差就是可能的结果了,遍历所有的i,用每次计算出的差值来更新结果 res 即可,参见代码如下:

```
class Solution {
public:
int smallestRangeII(vector& A, int K) {
sort(A.begin(), A.end());
int n = A.size(), res = A[n - 1] - A[0];
int left = A[0] + K, right = A[n - 1] - K;
for (int i = 0; i
Github 同步地址:

https://github.com/grandyang/leetcode/issues/910

类似题目:

Smallest Range I

参考资料:

https://leetcode.com/problems/smallest-range-ii/

https://leetcode.com/problems/smallest-range-ii/discuss/173377/C%2B%2BJavaPython-Add-0-or-2-*-K

https://leetcode.com/problems/smallest-range-ii/discuss/173389/simple-C%2B%2B-solution-with-explanation

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 910. Smallest Range II 最小区间之二的更多相关文章

  1. [LeetCode] 908. Smallest Range I 最小区间

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  2. LeetCode 910. Smallest Range II

    很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k ...

  3. 【LeetCode】910. Smallest Range II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【leetcode】910. Smallest Range II

    题目如下: 解题思路:我的思路是先找出最大值.对于数组中任意一个元素A[i]来说,如果A[i] + K 是B中的最大值,那么意味着从A[i+1]开始的元素都要减去K,即如果有A[i] + K > ...

  5. 910. Smallest Range II

    Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...

  6. [LeetCode] 632. Smallest Range Covering Elements from K Lists 覆盖K个列表元素的最小区间

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  7. [leetcode]632. Smallest Range最小范围

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  8. [LeetCode] 483. Smallest Good Base 最小的好基数

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  9. LeetCode 908 Smallest Range I 解题报告

    题目要求 Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...

随机推荐

  1. 常见的几种 Normalization 算法

    神经网络中有各种归一化算法:Batch Normalization (BN).Layer Normalization (LN).Instance Normalization (IN).Group No ...

  2. 如何在 Knative 中部署 WebSocket 和 gRPC 服务?

    作者 | 冬岛  阿里云容器平台工程师 导读:虽然说 Knative 默认就支持 WebSocket 和 gRPC,但在使用中会发现,有时想要把自己的 WebSocket 或 gRPC 部署到 Kna ...

  3. 【洛谷5438】【XR-2】记忆(数论)

    [洛谷5438][XR-2]记忆(数论) 题面 洛谷 题解 很好的一道题目. 我们首先把所有数的每个质因子的出现次数模二,也就是把最大的完全平方因子给除掉.然后剩下部分一样的就可以产生\(1\)的贡献 ...

  4. SpringBoot与Swagger整合

    1 SpringBoot与Swagger整合https://blog.csdn.net/jamieblue1/article/details/99847744 2 Swagger详解(SpringBo ...

  5. DateTimeComparer

    public int Compare(string x,string y) { DateTime xDate = DateTime.ParseExact(x, "MMMM", ne ...

  6. My time is limited

    Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma - whic ...

  7. SQL学习笔记之 数据库基础(一)

    数据库基础 数据库系统的组成:由数据库,数据库管理软件,数据库管理员DBA,支持数据库系统的硬件和软件组成,其中数据库管理员是对数据库进行规划.设计.维护.和监视的专业管理人员,在数据库系统中起着非常 ...

  8. git 使用 tortoisegit 解冲突

    git 解冲突需要注意的问题 弄清除冲突双向的修改意图,并在解决冲突时,同时处理两边的意图. 举例说明 A.txt 文件, 在 master 分支上,有一行文字(代码)是这样: 这是一段在 maste ...

  9. Vert.x HTTP 服务器与客户端

    编写HTTP 服务器与客户端 Vert.x让编写非阻塞的HTTP 服务器与客户端变得非常轻松. 创建HTTP 服务器 缺省状况: HttpServer server = vertx.createHtt ...

  10. U盘安装CentOS 7提示 “Warning: /dev/root does not exist, could not boot” 解决办法

    1.把U盘的Lable(即标签)修改成centos 2.在安装界面上按TAB键,修改启动路径,把”CENTOS\x207\x20x86_64″改成 “centos”