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
 

Approach #1: C++.

class Solution {
public:
int smallestRangeII(vector<int>& A, int K) {
int N = A.size();
sort(A.begin(), A.end());
int ans = A[N-1] - A[0]; for (int i = 0; i < A.size()-1; ++i) {
int a = A[i], b = A[i+1];
int high = max(A[N-1]-K, a+K);
int low = min(A[0]+K, b-K);
ans = min(ans, high-low);
} return ans;
}
};

  

Analysis:

We can formalize the above concept: if A[i] < A[j], we don't need to consider when A[i] goes down while A[j] goes up. This is because the interval (A[i] + K, A[j] - K) is a subset of (A[i] - K, A[j] + K) (here, (a, b) for a > b denotes (b, a) instead.)

That means that it is never worse to choose (up, down) instead of (down, up). We can prove this claim that one interval is a subset of another, by showing both A[i] + K and A[j] - K are between A[i] - K and A[j] + K.

For sorted A, say A[i] is the largest i that goes up. Then A[0] + K, A[i] + K, A[i+1] - K, A[A.length - 1] - K are the only relevant values for calculating the answer: every other value is between one of these extremal values.

910. Smallest Range II的更多相关文章

  1. [LeetCode] 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 ...

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

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

  3. LeetCode 910. Smallest Range II

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

  4. 【leetcode】910. Smallest Range II

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

  5. [Swift]LeetCode910. 最小差值 II | 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. Smallest Range II

    2020-01-21 21:43:52 问题描述: 问题求解: 这个题目还是有点难度的,感觉很巧妙也很难想到. 整体的思路如下: 1. 首先原问题等价于 +0 / + 2*K 2. 那么res = M ...

  7. [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 ...

  8. [LeetCode] Smallest Range 最小的范围

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

  9. [Swift]LeetCode632. 最小区间 | Smallest Range

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

随机推荐

  1. jQuery——表单异步提交

    如果不做任何处理,表单提交时会刷新页面,为了改善体验,可以使用jQuery做到异步提交表单:通过$("#form").serialize()将表单元素的数据转化为字符串,然后通过$ ...

  2. AttributeUsage

    [AttributeUsage] System.AttributeUsage声明一个Attribute的使用范围与使用原则. AllowMultiple 和 Inherited 参数是可选的,所以此代 ...

  3. Hibernate其它API

    ----------------siwuxie095 (一)Query 1.使用 Query 对象执行查询操作,不需要写 sql 语句,但是要写 hql 语句 (1)hql:即 Hibernate Q ...

  4. 为什么要使用href=”javascript:void(0);”

    为什么要使用href=”javascript:void(0);”   href=”javascript:void(0);”这个的含义是,让超链接去执行一个js函数,而不是去跳转到一个地址,而void( ...

  5. 377. Combination Sum IV 返回符合目标和的组数

    [抄题]: Given an integer array with all positive numbers and no duplicates, find the number of possibl ...

  6. porwedesigner 去掉引号

    PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大.小写 若要将 CDM 中将 Entity的标识符都设为指定的大小写,则可以这么设定: 打开cdm的情况下,进入Tool ...

  7. Metadata Service 最高频的应用 - 每天5分钟玩转 OpenStack(164)

    实现 instance 定制化,cloud-init(或 cloudbase-init)只是故事的一半,metadata service 则是故事的的另一半.两者的分工是:metadata servi ...

  8. Django调试工具django-debug-toolbar安装使用教程

    在网站开发中难免要调试页面,而使用django开发站点时,可以使用django-debug-toolbar来进行调试,安装这个插件很有用,我一开始是为了查看某个页面中所有的context变量值,当然你 ...

  9. pcl point merge

    http://pointclouds.org/documentation/tutorials/pairwise_incremental_registration.php#pairwise-increm ...

  10. 实践作业3:白盒测试----学习Junit框架DAY10.

    JUnit - 测试框架 首先应该了解什么是 Junit 测试框架? JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量.JUnit 测试框架能 ...