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 ...
随机推荐
- ACM学习历程——POJ1260 Pearls(动态规划)
Description In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces ...
- 【Python】String 字符串
1. split() split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 split()方法语法:str.split(str="" ...
- WCF知识点(应用WCF支持原生Socket访问, 原始字节流传输)
最近在做区域医疗中PIX时, 需要让PIX Manager同时支持HL7的V2和V3版本.思路是利用WCF来同时支持V2版本的c/s架构式的消息协议和V3版本WebService的Soap协议. 实 ...
- zk 04之 Zookeeper Api(java)与应用
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...
- 三 lambda表达式有什么用
(转载: https://mp.weixin.qq.com/s/-PHOc6p-qKJBktle28AUgA) 一: 直接把代码块赋值给变量 我们知道,对于一个Java变量,我们可以赋给其一个“值”. ...
- 获取CPU ID ,disk ID, MAC ID (windows ARM linux Mac)
windows 命令行获取CPU ID,可以用ShellExecute wmic cpu get processorid ProcessorId BFEBFBFF000506E3 开源库: 查询CPU ...
- S5pv210 HDMI 接口在 Linux 3.0.8 驱动框架解析
作者:liukun321 咕唧咕唧 日期:2014.1.18 转载请标明作者.出处:http://blog.csdn.net/liukun321/article/details/18452663 本文 ...
- grep的用法(CentOS7)及有关正则表达式的使用
环境准备:alias grep="grep --color" 1.grep以整行为单位进行处理,行中有的匹配显示出来 Last中取出符合root的行:grep '查找字符串' l ...
- docker添加阿里云镜像加速器
.docker添加阿里云镜像加速器 https://blog.csdn.net/chenjin_chenjin/article/details/86674521 .配置阿里云加速器 阿里云会根据账号生 ...
- IP简介2
本笔记介绍java net IP A 类 主要是大型的交换机 1-126 B类 128-191 分配给各个国家的典型的服务器 每个网段支持255*255个设备 每一个B类都可以分配444416个C ...