给定一个整数数组 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. 转载:Linux 安装Java

    1.到官网下载 jdk-8u131-linux-x64.tar.gz 官网地址:http://www.Oracle.com/technetwork/java/javase/downloads/jdk8 ...

  2. Redis Replication & Sentinel

    实践目标: Redis Replication 一主:192.168.1.104 双从:192.168.1.101 192.168.1.103 Sentinel:192.168.1.102 系统环境: ...

  3. charles for https

    To remotely capture http or https traffic with charles you will need to do the following: HOST - Mac ...

  4. 观察者模式(Observer、Subject、ConcreteSubject、ConcreteObserver)(监护、订阅)

    建立一种对象与对象之间的依赖关系,一个对象发生改变时将自动通知其他对象,其他对象将相应的作出反应. 在此发生改变的对象称之为观察目标(被观察者),而被通知的对象称为观察者,一个观察者目标可以对应多个观 ...

  5. 常见Idea插件

    一.Maven Helper Maven Helper用来查找和排除Jar包冲突的依赖关系. 安装: 打开Idea的Settings→Plugins→在输入框中输入“maven helper”→点击I ...

  6. react-native warn Failed to connect to development server using "adb reverse":

    react-native环境搭建中的问题 本文环境说明:开发环境:window10专业版,目标平台:Android react: , react-native: java: 1.8.0_221 pyt ...

  7. uva11401:Triangle Counting 递推 数学

    uva11401:Triangle Counting 题目读不清楚的下场就是多做两个小时...从1-n中任选3个不重复数字(不重复啊!!坑爹啊!)问能组成三角形的有多少个, 显然1~n能组成的三角形集 ...

  8. Kotlin 委托(1)类委托、变量委托注意事项

    1.官方文档 英文: https://kotlinlang.org/docs/reference/delegation.html https://kotlinlang.org/docs/referen ...

  9. java 判断数据类型和方法

    java 判断数据类型和方法 .我从SOLR查询中获取一个数据一,已知数据类型,是string或者int 或者其他 .我有一个方法(set方法),只有一个参数,但是我不知道参数的数据类型,可能是str ...

  10. MyEclipse使用总结——MyEclipse10.6 下添加jadClipse反编译插件[转]

    jad是一个使用比较广泛的Java反编译软件,jadClipse是jad在eclipse下的插件,下面像大家介绍下如何将jadclipse加入到MyEclipse10.X,9.X,8.X,6.X等各版 ...