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. WZY社区

    WZY社区是我自己做的一个网站,后面会详细更新,敬请关注!

  2. spring boot 开发 ajax返回值报错

    org.thymeleaf.exceptions.TemplateInputException: Error resolving template "succeed", templ ...

  3. 2018年全国多校算法寒假训练营练习比赛(第一场)E 恋与程序员

    https://www.nowcoder.com/acm/contest/67/E 思路: dfs 代码: #include<bits/stdc++.h> using namespace ...

  4. html css 伪样式

    伪类的分类及作用: 引自W3School教程伪元素的分类及作用: 这里就不进行多的描述,原文地址:http://www.it165.net/design/html/201406/2643.html

  5. Jersey 2.x 探索新建的工程

    如果用 Jersey maven archetype 成功创建了这个项目,那么在你当前的路径下就已经创建了一个名为simple-service项目.它包含了一个标准的Maven项目结构: 说明 文件目 ...

  6. The Number Games CodeForces - 980E (树, 贪心)

    链接 大意: 给定$n$节点树, 求删除$k$个节点, 使得删除后还为树, 且剩余点$\sum{2^i}$尽量大 维护一个集合$S$, 每次尽量添加最大的点即可 这样的话需要支持求点到集合的最短距离, ...

  7. laravel时间判断

    $now = Carbon::now(); if ($now >= '2019-01-02') { }

  8. CentOS7.6 Install TensorFlow

    1. install pip 1).  yum -y install epel-release 2).  yum install python-pip 3).  pip install --upgra ...

  9. gradle 编译 No such property: sonatypeUsername错误解决

    No such property: sonatypeUsername for class: org.gradle.api.publication.maven.internal.ant.DefaultG ...

  10. Linux的三种网络适配器

    Linux的三种网络适配器 分别为:桥接模式(Bridged),NAT模式,仅主机模式. 仅主机模式:        2>NAT模式 NAT 是虚拟机和本地网络使用一个ip地址 3>桥接模 ...