给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中。

在此过程之后,我们得到一些数组 B。

返回 B 的最大值和 B 的最小值之间可能存在的最小差值。

示例 1:

输入:A = [1], K = 0 输出:0 解释:B = [1]

示例 2:

输入:A = [0,10], K = 2 输出:6 解释:B = [2,8]

示例 3:

输入:A = [1,3,6], K = 3 输出:0 解释:B = [3,3,3] 或 B = [4,4,4]

提示:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 10000
  3. 0 <= K <= 10000
bool cmp1(int x, int y)
{
return x < y;
} class Solution {
public:
int smallestRangeI(vector<int>& A, int K) {
int len = A.size();
if(len == 1)
return 0;
sort(A.begin(), A.end(), cmp1);
int MIN = A[0];
int MAX = A[len - 1];
if(A[len - 1] - A[0] <= 2 * K)
return 0;
else
return A[len - 1] - A[0] - 2 * K; }
};

Leetcode908.Smallest Range I最小差值1的更多相关文章

  1. [Swift]LeetCode908. 最小差值 I | 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. [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 ...

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

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

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

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

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

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

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

  8. LuoguP4234_最小差值生成树_LCT

    LuoguP4234_最小差值生成树_LCT 题意: 给出一个无向图,求最大的边权减最小的边权最小的一棵生成树. 分析: 可以把边权从大到小排序,然后类似魔法森林那样插入. 如果两点不连通,直接连上, ...

  9. CCF CSP 201712-1 最小差值

    题目链接:http://118.190.20.162/view.page?gpid=T68 问题描述 试题编号: 201712-1 试题名称: 最小差值 时间限制: 1.0s 内存限制: 256.0M ...

随机推荐

  1. Java基础知识(多线程和线程池)

    新建状态: 一个新产生的线程从新状态开始了它的生命周期.它保持这个状态直到程序 start 这个线程. 运行状态:当一个新状态的线程被 start 以后,线程就变成可运行状态,一个线程在此状态下被认为 ...

  2. html的常用标签详解1

    1.<!DOCTYPE html> 文档声明,不算是标签,但是它可是不能少.这玩意是干什么用的呢? 它是向浏览器自报家门的,即告诉浏览器的解析器应该以什么样的文档类型定义(DTD)来解析它 ...

  3. DSMM之数据处理安全

    一.背景 数据安全生命周期分为采集.传输.存储.处理.交换.销毁几个阶段,其中数据处理阶段是整个周期的核心阶段,数据处理安全与否直接关系到整体数据安全.那么今天分享内容就是数据处理安全的相关要求和实现 ...

  4. opencv4 java投影

    工程下载 https://download.csdn.net/download/qq_16596909/11505994 比较适合与验证码的处理,毕竟八邻域降噪不能消除比较大的噪点,为了尽量减少噪点对 ...

  5. 015-WebDriver API

    1. 从定位元素开始 8种元素定位方法 id find_element_by_id( ) name find_element_by_name( ) tag find_element_by_tag_na ...

  6. zepto.js按需载入模板对象

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/Joyhen/article/details/34412103 Zepto.js 是支持移动WebKi ...

  7. Linux中如何安装mysql数据库

    安装mysql 1.解压源码压缩包 如果服务器可以上网也可以采用在线安装方式,在线安装操作简单具体见下面在线安装步骤 进入源码压缩包所在目录输入#tar -zxvf mysql-5.6.17-linu ...

  8. MySQL用户权限详细汇总

    1,MySQL权限体系 mysql 的权限体系大致分为5个层级:全局层级:全局权限适用于一个给定服务器中的所有数据库.这些权限存储在mysql.user表中.GRANT ALL ON .和REVOKE ...

  9. 利用Python覆盖图像的某一部分,即改变图形一块区域(Region)的RGBA值

    原图如下: 改变过后的图如下: 查阅API写法如下: from PIL import Image from PIL import ImageDraw pilim = Image.open('1.jpg ...

  10. Tensorboard在Win7下chrome无论如何无法连接的情况

    后记:其实发现原因后感觉自己很蠢,是自己开了一个软件叫adsafe,会屏蔽一些东西,所以我拼命的用自己的电脑ip都连不上,换成回环地址就好了,把软件关了也可以. 在无数种尝试后,终于在stackove ...