Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add xto 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
class Solution:
def smallestRangeI(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
return (max(A)-min(A)-2*K) if (max(A)-min(A)>2*K) else 0

  

[LeetCode&Python] Problem 908. Smallest Range I的更多相关文章

  1. 【Leetcode_easy】908. Smallest Range I

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

  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】908. Smallest Range I 解题报告(Python)

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

  4. [LeetCode&Python] Problem 744. Find Smallest Letter Greater Than Target

    Given a list of sorted characters letters containing only lowercase letters, and given a target lett ...

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

  6. [LeetCode&Python] Problem 598. Range Addition II

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

  7. 【leetcode】908. Smallest Range I

    题目如下: 解题思路:简单的不能再简单的题目了,对于任意一个A[i]来说,其可能的最小的最大值是A[i]-K,最大的最小值是A[i]+K.遍历数组,求出所有元素中最大的最小值和最小的最大值,两者之差( ...

  8. [LeetCode&Python] Problem 720. Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  9. [LeetCode&Python] Problem 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

随机推荐

  1. JavaScript 基础知识入门

    ​ js3种弹出框 alert消息对话框 var mychar=I love JavaScript;alert(mychar); confirm消息对话框 返回值:bool var mymessage ...

  2. java.lang.NoSuchMethodError问题处理

    出现这个问题一般是jar包冲突了,我找了很久没找到是哪个jar冲突了.最后用下面的这段代码,找到是哪个jar冲突了 /** * find jar file */ String LOCATION = & ...

  3. 监督学习--k近邻算法

    2017-07-20 15:18:25 k近邻(k-Nearest Neighbour, 简称kNN)学习是一种常用的监督学习方法,其工作机制非常简单,对某个给定的测试样本,基于某种距离度量找出训练集 ...

  4. B2B、B2C、C2C、O2O 和 P2P 的含义

    B2C(Business-to-Customer)商家对客户 我开一家公司卖东西,你来买,即B2C.生活中常用的比如我们经常在天猫旗舰店上面购物,天猫入驻的都是商家,而我们买东西的就是客户,这就是B2 ...

  5. spring-cloud: eureka之:ribbon负载均衡自定义配置(二)

    spring-cloud: eureka之:ribbon负载均衡自定义配置(二) 有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取 准备工作: 1.eureka服 ...

  6. ThreadLocal实现原理

      一.ThreadLocal介绍     这是一个线程的局部变量.也就是说,只有当前线程可以访问.既然是只有当前线程可以访问的数据,自然是线程安全的.     为每一个线程分配不同的对象,需要在应用 ...

  7. Nastya and King-Shamans CodeForces - 992E (线段树二分)

    大意: 给定序列a, 单点更新, 询问是否存在a[i]等于s[i-1], s为a的前缀和, a非负 考虑到前缀和的单调性, 枚举从1开始前缀和, 找到第一个大于等于s[1]的a[i], 如果相等直接输 ...

  8. Python在七牛云平台的应用(二)图片瘦身

    (一)七牛云平台的图片瘦身功能简介:(引用自官网) 针对jpeg.png格式图片 瘦身后分辨率不变,格式不变. 肉眼画质不变. 图片体积大幅减少,节省 CDN 流量 官网给的图片压缩率很高,官网给的「 ...

  9. ubuntu计划任务

    1.第一次编写计划任务,你输入crontab -l 会报错:no crontab for root 这个解决方法:输入crontab -e 2,第一次编写计划任务的时候你要输入select -edit ...

  10. xrat CC特征

    ["6", 11818669.0, 8326.0, 599.75, 19705.992496873696, 13.882451021258857, 0.07203338938265 ...