题目如下:

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

代码如下:

class Solution(object):
def smallestRangeI(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
minv,maxv = A[0] + K, A[0] - K
for i in A:
minv = min(minv,i+K)
maxv = max(maxv,i-K)
return max(0,maxv-minv)

【leetcode】908. Smallest Range I的更多相关文章

  1. 【LeetCode】908. Smallest Range I 解题报告(Python)

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

  2. 【Leetcode_easy】908. Smallest Range I

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

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

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

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

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

  5. 【leetcode】910. Smallest Range II

    题目如下: 解题思路:我的思路是先找出最大值.对于数组中任意一个元素A[i]来说,如果A[i] + K 是B中的最大值,那么意味着从A[i+1]开始的元素都要减去K,即如果有A[i] + K > ...

  6. 【LeetCode】163. Missing Range

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given a sorted integer array where the rang ...

  7. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

  8. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

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

  9. 【leetcode】1257. Smallest Common Region

    题目如下: You are given some lists of regions where the first region of each list includes all other reg ...

随机推荐

  1. @PostMapping

    @PostMapping映射一个POST请求 Spring MVC新特性 提供了对Restful风格的支持 @GetMapping,处理get请求 @PostMapping,处理post请求 @Put ...

  2. Android Fastboot 与 Recovery 和刷机 千山万水迷了鹿

    1. 首先来看下Android系统的分区:   Android系统的分区.jpg   Android分区解释.png 安卓系统一般把rom芯片分成7个区,如果再加上内置sd卡这个分区,就是8个: hb ...

  3. 【靶场练习_sqli-labs】SQLi-LABS Page-4 (Challenges)

    Less-54: ?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where tabl ...

  4. SQL各种JOIN

    JOIN(= INNER JOIN):返回匹配的结果,没有匹配则没结果: LEFT JOIN(= LEFT OUTER JOIN):返回匹配的与左表的所有数据: RIGHT JOIN(= RIGHT ...

  5. 初步认识pug

    一.初步认识pug 1.所谓的pug就是我们之前说的jade,也就是一种通过缩进的方式来编写代码的过程,在编译的过程中,我们不需要考虑标签是否闭合的问题.此外,用这种编译方式,加快了我们写代码的速度, ...

  6. python中匿名函数lamada函数的使用说明

    匿名函数lambda是指一类无需定义标识符(函数名 )的一类函数式或子程序.lambda函数可以 接受多个任意参数,并且返回单个表达式的值. 它的意义在于即插即用类型,不必定义名字,方便.它需要的返回 ...

  7. mongo 数据库存储

    mongo 数据库,获取有赞的数据. from app import mongo from app.external.yz.goods_api import YzGoodsApi from openp ...

  8. 让Tomcat支持php

    在服务器上php的安装:1.下载php-4.4.2 for windows的压缩包2.解压缩到D:\software\php4423.将D:\software\php442增加到环境变量PATH中4. ...

  9. Java并发AtomicLongArray类

    java.util.concurrent.atomic.AtomicLongArray类提供了可以原子读取和写入的底层long类型数组的操作,并且还包含高级原子操作. AtomicLongArray支 ...

  10. JS高级程序随笔二

    var person1={ toLoginString:function(){ return "lili"; }, toString2:function(){ return &qu ...