这是悦乐书的第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)的更多相关文章

  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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. Leetcode908.Smallest Range I最小差值1

    给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中. 在此过程之后,我们得到一些数组 B. 返回 B 的最大值 ...

  6. [LeetCode] Smallest Range 最小的范围

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  7. [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 ...

  8. 【Leetcode_easy】908. Smallest Range I

    problem 908. Smallest Range I solution: class Solution { public: int smallestRangeI(vector<int> ...

  9. 【LeetCode】632. Smallest Range 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...

随机推荐

  1. Android 虚拟机 程序安装目录

    Android应用安装涉及到如下几个目录:system/app系统自带的应用程序,无法删除.data/app用户程序安装的目录,有删除权限.安装时把apk文件复制到此目录.data/data存放应用程 ...

  2. Ruby 打印

    puts: 输出内容自动换行,转义后再输出(转义符),可传递多个参数puts("this is ge num=",a,"this is b=",b)   pri ...

  3. 项目log4j日志管理详解

    项目log4j日志管理详解 log4j日志系统在项目中重要性在这里就不再累述,我们在平时使用时如果没有特定要求,只需在log4j.properties文件中顶入输出级别就行了.如果要自定义输出文件,对 ...

  4. vector缩减容量

    在C++标准库容器vector的容量是不会自动的缩减的,也就是说删除元素操作,其引用.指针.迭代器也会继续有效.那么当在一个较大的vector中删除了大量的元素之后,其实际的size比较小,而其cap ...

  5. C# HttpRequest

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  6. html5--2.4新的布局元素(3)-section

    html5--2.4新的布局元素(3)-section 学习要点 了解section元素的语义和用法 通过实例理解section元素的用法 article元素和section元素的区别和共同点 art ...

  7. 分享知识-快乐自己:oracle表分区详解

    从以下几个方面来整理关于分区表的概念及操作: 1)表空间及分区表的概念: 2)表分区的具体作用: 3)表分区的优缺点: 4)表分区的几种类型及操作方法: 5)对表分区的维护性操作: 1):表空间及分区 ...

  8. Tips:PowerDesigner16.5 图表显示Code以及 Columns新增Commet显示

  9. HTml js 生成图片

    <script type="text/javascript"> function $(id) { return document.getElementById(id); ...

  10. CodeForces - 697F:Legen... (AC自动机+矩阵)

    Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney ...