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

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: 0
Explanation: B = [3,3,3] or B = [4,4,4]

Note:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 10000
  3. 0 <= K <= 10000
 思路:
很朴素的想法,扫描一趟即可知道最大值与最小值,然后再比较一下即可。
int smallestRangeI(vector<int>& A, int K)
{
int minV = A[], maxV = A[];
for(int i = ; i < A.size(); i++)
{
minV = min(minV, A[i]);
maxV = max(maxV, A[i]);
}
return max(((maxV - minV) - * K),);
}

[leetcode-908-Smallest Range I]的更多相关文章

  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 908 Smallest Range I 解题报告

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

  3. 【Leetcode_easy】908. Smallest Range I

    problem 908. Smallest Range I solution: class Solution { public: int smallestRangeI(vector<int> ...

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

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

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

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

  7. 【LeetCode】908. Smallest Range I 解题报告(Python)

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

  8. [LeetCode&Python] Problem 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 ...

  9. 【leetcode】908. Smallest Range I

    题目如下: 解题思路:简单的不能再简单的题目了,对于任意一个A[i]来说,其可能的最小的最大值是A[i]-K,最大的最小值是A[i]+K.遍历数组,求出所有元素中最大的最小值和最小的最大值,两者之差( ...

  10. 解题报告-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. Oracle 执行计划的查看方式

    访问数据的方法:一.访问表的方法:1.全表扫描,2.ROWID扫描                                二.访问索引的方法:1.索引唯一性扫描,2.索引范围扫描,3.索引全扫 ...

  2. Read a large file with python

    python读取大文件 较pythonic的方法,使用with结构 文件可以自动关闭 异常可以在with块内处理 with open(filename, 'rb') as f: for line in ...

  3. Java---Huffman树的实现

    什么是哈弗曼树 1.哈弗曼树是最优二叉树,树的带权路径长度最小的一个二叉树. 2.带权路径长度为根节点到该节点的路径长度和该节点权重的乘积.3.路径长度为当前节点到另一个节点所经过的分支的个数(边的个 ...

  4. Ubuntu18.04挂载exfat格式移动硬盘

    1.安装exfat-fuse 命令:sudo apt-get install exfat-fuse 2.重新插拔移动硬盘,即可识别 查看挂载命令:lsblk

  5. ionic 环境搭建

    1.安装nodejs (8.4.0) 下载地址 https://nodejs.org/zh-cn/ 2.Java jdk  版本号  jdk1.8.0_121 3.安装 cordova npm ins ...

  6. kafka搭建到配置borker集群(项目开发-区块链)

    (以下分享了搭建kafka需要使用到的命令,差不多齐了,这里没有提到kafka-manager ,同学可以在网上自行查找) 最近公司的项目比较紧,先说下使用kafka的用处: 要替代原来 撮合引擎发数 ...

  7. 使用for in 循环数据集

    在DELPHI没有FOR IN的语法时,我们要使用如下代码枚举数据集中的每个内容: cds.First; while not cds.eof do begin ... cds.Next; end; 最 ...

  8. Home Assistant系列 -- 自动语音播报天气

    在树莓派上要让 Home Assistant  发声朗读 ,从硬件上我们需要 3.5mm插口音箱(耳机)或usb口音箱(耳机).软件方面 我们需要一个媒体播放器以及 tts ( 将文字转化为语音的服务 ...

  9. Asp.Net Core存储Cookie不成功

    Asp.Net Core存储Cookie不成功 Asp.Net Core2.1生成的项目模板默认实现了<>,所以设置存储Cookie需要做一些处理. 1.第一种是在Startup的Conf ...

  10. 批量复制windows文件夹下所有文件名

    第一步,打开文件夹 第二步,在该文件夹下新建一个txt文件,然后将“.txt”后缀名修改为“.bat” txt文件内容“DIR *.* /B >LIST.TXT” 第三步,双击“.bat”,直接 ...