LeetCode.908-最小差值 1(Smallest Range I)
这是悦乐书的第348次更新,第372篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第213题(顺位题号是908)。给定一个整数数组A,对于每个整数A[i],我们可以选择任何x,其中-K <= x <= K,并将x的值加到A[i]上。在这个过程之后,A变成了新数组B.
返回B的最大值和B的最小值之间的最小可能差值。例如:
输入:A = [1],K = 0
输出:0
说明:B = [1]
输入:A = [0,10],K = 2
输出:6
说明:B = [2,8]
输入: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
02 解题
题目要求我们计算B数组中最大值和最小值的最小可能差值,而B数组是由A数组中每一个元素加上K后得到的。
要想最大值和最小值的差值最小,即最大值、最小值无限接近,最理想状态是最大值等于最小值,其差值为0。
所以,我们只需要找到A里面的最大值、最小值,将最大值减去x的最大值,即K,将最小值加上x的最大值,让最大值、最小值的数值更加接近。
另外,最大值和最小值的最小可能差值是不能小于0的,最小只能到0。
public int smallestRangeI(int[] A, int K) {
int max = -1, min = 10001;
for (int num : A) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
if ((max-K)-(min+K) < 0) {
return 0;
}
return (max-K)-(min+K);
}
03 小结
算法专题目前已连续日更超过六个月,算法题文章216+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.908-最小差值 1(Smallest Range I)的更多相关文章
- [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] 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 ...
- Leetcode908.Smallest Range I最小差值1
给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中. 在此过程之后,我们得到一些数组 B. 返回 B 的最大值 ...
- [LeetCode] Smallest Range 最小的范围
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- [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 ...
- 【Leetcode_easy】908. Smallest Range I
problem 908. Smallest Range I solution: class Solution { public: int smallestRangeI(vector<int> ...
- 【LeetCode】632. Smallest Range 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...
随机推荐
- save create
其中 create 和 create!就等於 new 完就 save 和 save!,有無驚嘆號的差別 在於 validate 資料驗證不正確的動作,無驚嘆號版本會回傳布林值(true 或 false ...
- 解决使用mybatis做批量操作时发生的异常:Parameter '__frch_item_0' not found. Available parameters are [list] 记录
本文主要描述 使用mybatis进行批量更新.批量插入 过程中遇到的异常及总结: 首先贴出使用批量操作报的异常信息: java.lang.RuntimeException: org.mybatis.s ...
- react-native修改android包名
安卓已包名作为应用的唯一id,相对iOS来说改起来就不是那么方便,但为了能正式发布自己的应用,还是得改过来. 假设包名为com.exease.etd.objective,以下地方需要修改. 首先是两个 ...
- 算法(Algorithms)第4版 练习 1.3.25 1.3.24
代码实现: //1.3.24 /** * remove the node following the node x * (and does nothing if the argument or the ...
- matlab之细胞数组
学习matlab的一个博客:https://blog.csdn.net/smf0504/article/details/51814362 Matlab从5.0版开始引入了一种新的数据类型—细胞( ce ...
- unable to create new native thread 问题
ulimit -a ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling pr ...
- L85
Surgical Never Events Happen Nevertheless Surgeons call them "never events", because they ...
- 1072 Gas Station (30)(30 分)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- RT-Thread的线程(任务)处理 rt_thread_create/rt_thread_init区别
RT-Thread中使用线程这个概念,而不是任务.两者相似,我在这里把他的线程当作任务来理解了 1.任务处理: 动态任务相关API 创建任务:rt_thread_create函数,创建任务之后会返回r ...
- lwip 分析一
一.接收端 1.通过ehternetif_input void ethernetif_input(struct netif *netif) { struct ethernetif *etherne ...