[LeetCode&Python] Problem 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 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
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的更多相关文章
- 【Leetcode_easy】908. Smallest Range I
problem 908. Smallest Range I solution: class Solution { public: int smallestRangeI(vector<int> ...
- [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】908. Smallest Range I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- [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 ...
- 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&Python] Problem 598. Range Addition II
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- 【leetcode】908. Smallest Range I
题目如下: 解题思路:简单的不能再简单的题目了,对于任意一个A[i]来说,其可能的最小的最大值是A[i]-K,最大的最小值是A[i]+K.遍历数组,求出所有元素中最大的最小值和最小的最大值,两者之差( ...
- [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 ...
- [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 ...
随机推荐
- DrawDibDraw__ZC测试
ZC: 先把 自己尝试成功的代码 记录下来,不要 弄没了之后 又忘了怎么弄... ZC: 代码 有点乱,没整理.没写 哪些是 原来MFC里面的 哪些是我自己写的,参考上一篇文章来看吧 1.VC6 的一 ...
- 将数组划分成连续子序列 Split Array into Consecutive Subsequences
2018-08-04 20:47:43 问题描述: 问题描述: 本题需要的是将一个数组划分成子序列,保证每个子序列是连续的,并且长度要大于等于3. 解题思路是使用贪心算法,首先对数组中的数字进行计数, ...
- Hibernate实例
Hibernate实例 一.Hibernate简介 Hibernate是简化项目中连接数据库的一个框架工具 Hibernate是Java领域类技术成熟稳定的ORM框架 * ORM是对象关系映射 * 使 ...
- WPF StoryBoard用法
时间:2011-06-15 21:26来源:百度空间 作者:shichen4 点击: 次 StoryBoard使用,Xaml转cs代码 Canvas.Triggers EventTriggerRout ...
- Assert.IsNotNull 方法(判断对象不为NULL)
Assert.IsNotNull 方法 Visual Studio 2012 其他版本 Visual Studio 2010 Visual Studio 2008 Visual Studio 20 ...
- English trip -- Review Unit3 Family 家人
Words daughter grandfather grandmother husband wife uncle aunt brother sister Who is ...? Loki's ... ...
- Maven部署web应用到远程服务器
Maven部署web应用到远程服务器 找到了一个很详细的地址:http://www.mkyong.com/maven/how-to-deploy-maven-based-war-file-to-tom ...
- 第 6 章 —— 依赖项注入(DI)容器 —— Ninject
有些读者只想理解 MVC 框架所提供的特性,而不想介入开发理念与开发方法学.笔者不打算让你改变 —— 这属于个人取向,而且你知道交付优质项目需要的是什么. 建议你至少粗略第看一看本章的内容,以明白哪些 ...
- axis2的WebService无法注入Service层类
package com.vrv.paw.axiswebservices; import org.springframework.web.context.ContextLoader; import or ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...