Leetcode908.Smallest Range I最小差值1
给定一个整数数组 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 <= A.length <= 10000
- 0 <= A[i] <= 10000
- 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的更多相关文章
- [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 ...
- [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 ...
- [LeetCode] Smallest Range 最小的范围
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- [Swift]LeetCode632. 最小区间 | Smallest Range
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- [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 ...
- [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 ...
- [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 ...
- LuoguP4234_最小差值生成树_LCT
LuoguP4234_最小差值生成树_LCT 题意: 给出一个无向图,求最大的边权减最小的边权最小的一棵生成树. 分析: 可以把边权从大到小排序,然后类似魔法森林那样插入. 如果两点不连通,直接连上, ...
- CCF CSP 201712-1 最小差值
题目链接:http://118.190.20.162/view.page?gpid=T68 问题描述 试题编号: 201712-1 试题名称: 最小差值 时间限制: 1.0s 内存限制: 256.0M ...
随机推荐
- springboot拦截器的拦截配置和添加多个拦截器
在spring2.0之前的版本大部分都采用extends WebMvcConfigurerAdapter,把拦截器配置成一个bean,具体的方法,我不细说,网上一大堆.而在spring2.0之后,这个 ...
- mybatis 中 if else 用法
mybaits 中没有 else 要用 chose when otherwise 代替 下面就是MyBatis中的if....else...表示方法 <choose> <when t ...
- deepfake安装python常用命令
pip install -r requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/ python -m p ...
- noi.openjudge 二分法求函数的零点
二分法求函数的零点 总时间限制: 1000ms 内存限制: 65536kB 描述 有函数:f(x) = x5 - 15 * x4+ 85 * x3- 225 * x2+ 274 * x - 121 已 ...
- 升级gitk后,Error in startup script: unknown color name "lime"
$ gitkError in startup script: unknown color name "greeen" (processing "-fore" o ...
- Tool-MySQL-SQLyog:SQLyog
ylbtech-Tool-MySQL-SQLyog:SQLyog SQLyog 是一个快速而简洁的图形化管理MYSQL数据库的工具,它能够在任何地点有效地管理你的数据库,由业界著名的Webyog公司出 ...
- 深喉起底APP线下预装市场,如何一夜间拥有千万用户
注:预装对于中国的移动互联网创业者有多重要?i黑马知道这样一个内幕,某商务告诉我他们公司的前2000万用户就是靠预装打下来的,总部在北京,直接派驻商务长期扎根在深圳搞定手机厂商.而这家公司初期发展得益 ...
- goland快捷键使用
查找替换: 格式化代码块:ctrl+alt+L将选中的行自动对齐:ctrl+alt+I优化没必要的imports:ctrl+alt+O展开代码块:ctrl+“+”展开文件中所有代码块:ctrl+shi ...
- [Array] 566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- 配置android studio环境-Hello world
运行hello world demo 运行D:\Program Files\Android\Android Studio\bin 选择创建一个项目 出现一系列的选择 如果没有出现下列问题,直接跳过 备 ...