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 ...
随机推荐
- Css if hack条件语法
Css if hack条件语法 <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> ...
- MySQL8.0.17 - 初探 Clone Plugin
MySQL8.0.17推出了一个重量级的功能:clone plugin.允许用户可以将当前实例进行本地或者远程的clone.这在某些场景尤其想快速搭建复制备份或者在group replication里 ...
- autoreleasing的用法介绍:
在c/c++,objective-c内存管理中有一条是:谁分配谁释放. __autoreleasing则可以使对像延迟释放.比如你想传一个未初始化地对像引用到一个方法当中,在此方法中实始化此对像,那么 ...
- 用Python输出一个杨辉三角的例子
用Python输出一个杨辉三角的例子 这篇文章主要介绍了用Python和erlang输出一个杨辉三角的例子,同时还提供了一个erlang版杨辉三角,需要的朋友可以参考下 关于杨辉三角是什么东西,右转维 ...
- PAT甲级——A1044 Shopping in Mars
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- retrying模块的使用
安装模块:pip3 install retrying 使用方式: 使用retrying模块提供的retry模块 通过装饰器的方式使用,让装饰器的函数反复的执行 retry可以传入参数stop_max_ ...
- vim编辑器操作②
本文主要介绍vim的常用编辑命令: 字符编辑: x:删除光标所在处的字符: #x:删除光标所在处起始的#个字符: 替换命令: r:替换光标所在处的字符: rCHAR; 例如:替换list中的l为大写L ...
- mysql本地导入数据
1.获得一个超级权限的用户 grant all on *.* to root@'127.0.0.1' identified by 'root';# 因为我想在本地导入数据,而数据就在本地.# 有时候, ...
- TZ_05_Spring_annotation常见注解
Spring常用的注解大全和解释 注解 解释 @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类 ...
- sublime中用less实现css预编译
实现css预编译的方式有很多,听说glup很流行而且功能也很强大,但是就目前的工作而言,仅要css预编译和YUIcompress就够了,接下来切入正题 Less 是一门 CSS 预处理语言,它扩展了 ...