这是悦乐书的第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。

  1. public int smallestRangeI(int[] A, int K) {
  2. int max = -1, min = 10001;
  3. for (int num : A) {
  4. if (num > max) {
  5. max = num;
  6. }
  7. if (num < min) {
  8. min = num;
  9. }
  10. }
  11. if ((max-K)-(min+K) < 0) {
  12. return 0;
  13. }
  14. return (max-K)-(min+K);
  15. }

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. java.sql.SQLException: Illegal connection port value '3306:success'

    严重: Servlet.service() for servlet jsp threw exceptionjava.sql.SQLException: Illegal connection port ...

  2. 插头dp小结

    插头dp: \(A:\)插头dp是什么? \(B:\)一种基于连通性状态压缩的动态规划问题 \(A:\)请问有什么应用呢? \(B:\)各种网格覆盖问题,范围允许状压解决,常用于计算方案数与联通块权值 ...

  3. 3D 图片播放焦点图插件Adaptor

    在线演示 本地下载

  4. git删除远程分支【转】

    本文转载自:https://my.oschina.net/tsingxu/blog/84601 如果不再需要某个远程分支了,比如搞定了某个特性并把它合并进了远程的 master 分支(或任何其他存放  ...

  5. 第三届蓝桥杯决赛c++b组

    1.星期几 [结果填空] (满分5分)     1949年的国庆节(10月1日)是星期六.      今年(2012)的国庆节是星期一.     那么,从建国到现在,有几次国庆节正好是星期日呢? 只要 ...

  6. C/C++的四大内存分区和常量的存储位置

    原文:https://blog.csdn.net/k346k346/article/details/45592329 正确的理解C/C++程序的内存分区,是合格程序猿的基本要求. 网络上流形两大版本内 ...

  7. 1>/dev/null 2>&1 & 意思解析

    原文:https://jingyan.baidu.com/article/6dad5075334e26a123e36e31.html 用 /dev/null 2>&1 这样的写法.这条命 ...

  8. [acm]HDOJ 1200 To and Fro

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1200 简单字符串处理,找规律 /* 11509672 2014-08-21 11:32:55 Acc ...

  9. BZOJ-4003:城池攻占(可并堆+lazy标记)

    小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 到 n 的整数表示.除 号城池外,城池 i 会受到另一座城池 fi 的管辖, 其中 fi <i.也就是 ...

  10. AtCoder Regular Contest 072 E:Alice in linear land

    题目传送门:https://arc072.contest.atcoder.jp/tasks/arc072_c 题目翻译 给你一个数组\(D\),然后给你一个操作序列\(d\),每次操作可以将\(D\) ...