作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/smallest-range-i/description/

题目描述

Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i].

After this process, we have some array B.

Return the smallest possible difference between the maximum value of B and the minimum value of B.

Example 1:

Input: A = [1], K = 0
Output: 0
Explanation: B = [1]

Example 2:

Input: A = [0,10], K = 2
Output: 6
Explanation: B = [2,8]

Example 3:

Input: A = [1,3,6], K = 3
Output: 0
Explanation: B = [3,3,3] or B = [4,4,4]

Note:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 10000
  3. 0 <= K <= 10000

题目大意

A中的每个数值都可以加上另外一个数字x (-K <= x <= K). 这样对A中所有的数字进行了上述操作之后,求得到的新的数组B的最大值和最小值之差。

解题方法

数学计算

要求的是新的数组最大值和最小值之差,所以,我们应该把所有的数字向中间靠拢。即A中的最小值+K, 最大值-K,判断这样操作之后,能否有重叠,如果能重叠所求的结果就是0;如果不能重叠,所求的结果就是两者的差值。

时间复杂度是O(N),空间复杂度是O(1).

代码如下:

class Solution(object):
def smallestRangeI(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
return max(max(A) - min(A) - 2 * K, 0)

参考资料:

日期

2018 年 9 月 23 日 —— 今天是实验室第一个打卡的
2018 年 11 月 5 日 —— 打了羽毛球,有点累

【LeetCode】908. Smallest Range I 解题报告(Python)的更多相关文章

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

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

  3. 【LeetCode】910. Smallest Range II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

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

  5. 【LeetCode】370. Range Addition 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...

  6. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  7. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  8. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  9. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

随机推荐

  1. MacBookpro安装VMware Fusion虚拟机,并安装win7 64位系统

    1.准备好安装用的东西(准备好正确的东西,安装路上就成功了一半)(1)VMware Fusion 附带注册机生成注册码,链接: https://pan.baidu.com/s/13Qm9zPOFjFt ...

  2. 拒绝恶意同构ssh登陆服务器脚本

    #!/bin/bash #Deny specified IP access #IP:who is fail to login sever SECURE_LOG=/var/log/secure #通过s ...

  3. 解决UE4项目编辑器启动时出现LogLinker: Warning: Failed to load '/Game/FirstPersonBP/FirstPersonOverview': Can't find file.

    UE4版本:4.24.3源码编译版本 Windows10 + VS2019环境 LogLinker: Warning: Failed to load '/Game/FirstPersonBP/Firs ...

  4. Flink 实践教程-进阶(2):复杂格式数据抽取

    作者:腾讯云流计算 Oceanus 团队 流计算 Oceanus 简介 流计算 Oceanus 是大数据产品生态体系的实时化分析利器,是基于 Apache Flink 构建的具备一站开发.无缝连接.亚 ...

  5. k8s集群中部署Rook-Ceph高可用集群

    先决条件 为确保您有一个准备就绪的 Kubernetes 集群Rook,您可以按照这些说明进行操作. 为了配置 Ceph 存储集群,至少需要以下本地存储选项之一: 原始设备(无分区或格式化文件系统) ...

  6. springcloud - alibaba - 3 - 整合config - 更新完毕

    0.补充 1.需求 如果我有这么一个请求:我想要gitee中的配置改了之后,我程序yml中的配置也可以跟着相应产生变化,利用原生的方式怎么做?一般做法如下: 而有了SpringCloud-alibab ...

  7. Hive(三)【DDL 数据定义】

    目录 一.DDL数据定义 1.库的DDL 1.1创建数据库 1.2查询数据库 1.3查看数据库详情 1.4切换数据库 1.5修改数据库 1.6删除数据库 2.表的DDL 2.1创建表 2.2管理表(内 ...

  8. Vue相关,diff算法。

    1. 当数据发生变化时,vue是怎么更新节点的? 要知道渲染真实DOM的开销是很大的,比如有时候我们修改了某个数据,如果直接渲染到真实dom上会引起整个dom树的重绘和重排,有没有可能我们只更新我们修 ...

  9. FileReader (三) - 网页拖拽并预显示图片简单实现

    以下是一个很贱很简单的一个 在网页上图拽图片并预显示的demo. 我是从https://developer.mozilla.org/en-US/docs/Web/API/FileReader#Stat ...

  10. Shell学习(十)——du、df命令

    一.du 命令 1.命令格式: du [选项][文件] 2.命令功能: 显示每个文件和目录的磁盘使用空间. 3.命令参数: -a或-all 显示目录中个别文件的大小. -b或-bytes 显示目录或文 ...