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 ...
随机推荐
- iPad actionsjeet
在iphone和ipad上使用UIActionShee控件t的效果会不一样,在苹果的官方文档中有相关说明: 在ipad上使用UIActionSheet控件改控件不再从底部弹出,而是从屏幕中间弹出与UI ...
- 《CSS权威指南(第三版)》---第一章 CSS和文档
主要学习的知识是怎么把CSS和HTML文档关联: 1.这是默认的样式表 <link rel="stylesheet" href="" type=" ...
- LoadRunner监控图表与配置(一) 监控与图表
1.“Monitoer”菜单-“Online Graphs”-“Open a new graph”打开监视图表列表. 实际上这些监视图表已经在Available Graphs中显示了出来. 2.常用监 ...
- hdu 6006
HDU - 6006 Engineer Assignment 我参考了这份题解. 贴上我比较拙的代码,留念一下. /** * 想到状态压缩的dp问题就解决了一半. */ #include <st ...
- 【Lintcode】028.Search a 2D Matrix
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- HDU1828:Picture
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:http://acm.hdu.edu.cn/showproblem.php? ...
- 记一次keepalived脑裂问题查找
在自己环境做keepalived+Redis实验时,当重启了备用redies机器后,发现两台redies主机都拿到了VIP [root@redis2 ~]# ip addr list 1: lo: & ...
- Code:Base64 编码/解码
ylbtech-Code:Base64 编码/解码 1. C#返回顶部 1.编码 byte[] inArray = new byte[msgTxt.Length]; int x; ; x < m ...
- db2 command line notes
db2ilist - list instances db2 attach to <instance> user <username> using <password> ...
- mvvm 模板中事件没有执行的解决方案
今天在wpf设计的过程中,用到了listview,数据绑定在模板中进行,其中有个按钮的click事件,一直不执行,很奇怪,找了很久才找到解决办法,原因还是暂时不清除: <ListView x:N ...