【LeetCode】908. Smallest Range I 解题报告(Python)
作者: 负雪明烛
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 <= A.length <= 10000
- 0 <= A[i] <= 10000
- 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)的更多相关文章
- 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 ...
- [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 ...
- 【LeetCode】910. Smallest Range II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】370. Range Addition 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
随机推荐
- C语言 fastq文件转换为fasta文件
目前只能处理短序列,若要处理长序列,可按照https://www.cnblogs.com/mmtinfo/p/13036039.html的读取方法. 1 #include <stdio.h> ...
- Linux生产应用常见习题汇总
1.如果想修改开机内核参数,应该修改哪个文件? C A./dev/sda1 (scsi sata sas,是第1块盘的第1个分区) B./etc/fstab (开机磁盘自动挂载配置文件) C./etc ...
- Oracle-like 多条件过滤以及and or用法
1.select * from file where DOC_SUBJECT not like '%测试%' and (DOC_STATUS like '待审' or DOC_STATUS li ...
- mysql order by 多个字段排序实现组内排序
总结:大组在前,小组在后,计量值再最后,即可实现组内排序:下边是参考别人的具体实例: 工作中需用到order by 后两个字段排序,但结果却产生了一个Bug,以此备录. [1]复现问题场景 为了说明问 ...
- 10.Power of Two-Leetcode
Given an integer, write a function to determine if it is a power of two. class Solution { public: bo ...
- 34、在排序数组中查找元素的第一个和最后一个位置 | 算法(leetode,附思维导图 + 全部解法)300题
零 标题:算法(leetode,附思维导图 + 全部解法)300题之(34)在排序数组中查找元素的第一个和最后一个位置 一 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: / ...
- Notepad++【远程操作linux文件】
目录 目的 预期效果 操作步骤 1.打开插件 2.安装NppFTP 3.连接远程主机 注意 目的 通过Notepad++远程登录linux主机,修改配置文件 预期效果 在Notepad++上登录lin ...
- 【Swift】CoreData的使用
CoreData只是iOS数据持久化的其中一个方法,所有数据持久化如下 1.plist文件(属性列表),通常用于储存用户设置,也zhi可以用于存储捆绑的信息: 2.preference(偏好设置),常 ...
- Android: EditText设置属性和设置输入规则
1.EditText输入限制规则 在xml:EditText 设置属性 android:digits="ABCDE123&*" ABCDE123&*是你的限制规则 ...
- Thymeleaf标准表达式
Thymeleaf的官网为: http://www.thymeleaf.org/ 一.变量表达式${-} 使用${-}括起来的表达式,称为变量表达式.该表达式的内容会显示在HTML标签体文本处. 该表 ...