给定一个整数数组 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. 目标检测从入门到精通—SPP-Net详细解析(三)

    SPP-Net网络结构分析 Author:Mr. Sun Date:2019.03.18 Loacation: DaLian university of technology 论文名称:<Spa ...

  2. grant

    # 添加超级用户 grant all privileges on *.* to 'dump_tmp'@'10.10.10.10' identified by 'dump_tmp'; grant all ...

  3. Django项目:CRM(客户关系管理系统)--82--72PerfectCRM实现CRM动态菜单和角色

    #models.py # ————————01PerfectCRM基本配置ADMIN———————— from django.db import models # Create your models ...

  4. leyou_06_Nginx的自启

    1.在linux系统的/etc/init.d/目录下创建nginx文件 vim /etc/init.d/nginx 添加以下内容 #!/bin/sh # # nginx - this script s ...

  5. 深入浅出 Java Concurrency (23): 并发容器 part 8 可阻塞的BlockingQueue (3)[转]

    在Set中有一个排序的集合SortedSet,用来保存按照自然顺序排列的对象.Queue中同样引入了一个支持排序的FIFO模型. 并发队列与Queue简介 中介绍了,PriorityQueue和Pri ...

  6. c++ stream操作杂记

    包含简单的读写文件,供初学者入门,stream目前包含三种,终端,文件,内存,注意宽字节. ) ; ) ; ] ; } pasting

  7. MySQL实战总结

    极客上买了<MySQL实战45讲>,用导图大致总结后,跟大家分享下

  8. python函数当容器

    def func(): pass func2 = func func2() i = [func,func2] for a in i: a() 函数名就是内存地址,加()代表执行

  9. Chapter 1 线性表

    Preface: 这部分主要是我考研之前的总结,留作以后用到时再翻出来看看.都是基础的知识点,望各(Da)位(Lao)不喜勿喷,欢迎指正. 适用人群:考研复习过一遍数据结构的,可以看看,查漏补缺(顺便 ...

  10. MyBatis框架的文件配置

    第一步:log4j.properties的配置 原因:Mybatis的日志输出是依赖与log4j的,所以必须要配置 # Global logging configuration log4j.rootL ...