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 ...
随机推荐
- mysql错误:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated
今天迁移django数据库的时候,跑程序的时候出现这样的错误: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY cla ...
- Java对象 的创建与构造方法
一.创建对象的四种方法: a. new语句: b. 利用反射,调用描述类的Class对象的newInstance()实例方法: c. 调用对象的clone(): d. 反序列化: 其中new 和 ne ...
- webpack为什么加载不了css?
原文地址: https://segmentfault.com/q/1010000005099261 这个app是用react写的. webpack的loader设置是这样的 module:{ load ...
- Kafka0.7运行时报错 kafka/javaapi/consumer/ConsumerConnector : Unsupported major.minor version 51.0 解决
目前中央库中 org.apache.kafka 是用jdk1.7编译的, 故跑在1.6的jvm中会报错 解决方案: 1. 下载kafka源码, 本地sbt进行install, 编译前 java -ve ...
- 分享知识-快乐自己:Hibernate各种查询操作
Hibernate各种查询操作(一) 测试数据库如下: t_sort表: t_good表: 一.对象导航方式查询 查询所有食品类下面 ...
- 常见css兼容问题
链接的虚线框问题 <!-- html --> <a class="noDashedBox" href="#"><img src=& ...
- swoole_table
Memory Swoole提供了7个内存操作的模块,在多进程编程中可以帮助开发者实现一些特殊的需求. swoole_table 是基于共享内存和锁实现的超高性能,并发数据结构
- win装wamp
前传: 刚换了工作环境,从原来的全mac转到全windows,很不适应,简单的wamp鼓捣了半天 环境: win 7 旗舰版 ,wamp ,下载地址: http://pan.baidu.com/s/1 ...
- codeforces 658B B. Bear and Displayed Friends(优先队列)
题目链接: B. Bear and Displayed Friends time limit per test 2 seconds memory limit per test 256 megabyte ...
- hdu-2169 Computer(树形dp+树的直径)
题目链接: Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...