【leetcode】908. Smallest Range I
题目如下:

解题思路:简单的不能再简单的题目了,对于任意一个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的更多相关文章
- 【LeetCode】908. Smallest Range I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- 【Leetcode_easy】908. Smallest Range I
problem 908. Smallest Range I solution: class Solution { public: int smallestRangeI(vector<int> ...
- 【LeetCode】632. Smallest Range 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...
- 【LeetCode】910. Smallest Range II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】910. Smallest Range II
题目如下: 解题思路:我的思路是先找出最大值.对于数组中任意一个元素A[i]来说,如果A[i] + K 是B中的最大值,那么意味着从A[i+1]开始的元素都要减去K,即如果有A[i] + K > ...
- 【LeetCode】163. Missing Range
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given a sorted integer array where the rang ...
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】1257. Smallest Common Region
题目如下: You are given some lists of regions where the first region of each list includes all other reg ...
随机推荐
- The main Method
The main Method You can call static methods without having any objects. For example, you never const ...
- Web核心之Request对象
HTTP协议中Request请求部分格式 //请求行(这种是POST类型的请求) POST /HttpServleLogin.html HTTP/1.1 //请求头(User-Agent里有Firef ...
- createElement 函数
我们知道,vue函数的渲染其实是由render函数的回调函数createElement 来创建的虚拟dom,那么它到底是怎么创建组件的? 尚未理解透彻[捂脸],有待补充,参考如下: https://w ...
- npm 常见错误记录
1.Module build failed: ReferenceError: Unknown plugin "import" specified in "base&quo ...
- 【Linux】grep显示匹配行的上下几行的用法
打印匹配行的前后5行 grep -5 ‘something’ file 打印匹配行的前后5行 grep -C 5 ‘something’ file 打印匹配行的后5行 grep -A 5 ‘somet ...
- HDU 4812 (点分治)
题目:https://vjudge.net/contest/307753#problem/E 题意:给你一颗树,树上每个点都有个权值,现在问你是否存在 一条路径的乘积 mod 1e6+3 等于 k的 ...
- Critical Links
UVA 796 Critical Links http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#overview 题目大意:给你一 ...
- JS继承 实现方式
JS中继承方式的实现有多种方法,下面是比较推荐的方法,其它继承方式可做了解: function object(o) { function F() {} F.prototype = o; return ...
- 【原】git命令行查看全部分支与远程分支不同步问题
git branch -a 查看全部分支 git fetch 重新拉一下分支
- Microsoft Office Excel
解除合并,并复制原始值到每一个解除合并后的单元格 对齐方式 -> 合并后居中 -> 取消单元格合并 编辑 -> 查找和选择 -> 定位条件 -> 空值 输入=然后按↑选择 ...