【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 ...
随机推荐
- Docker常规操作
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11601853.html Docker 常⽤命令 镜像相关 • docker pull <imag ...
- 51nod 1384:全排列(STL)
题目链接 记住next_permutation函数的用法,另外string在这里比char[]慢好多啊.. //#include<bits/stdc++.h> //using namesp ...
- 树结构遍历节点名字提取,这里提取的是el-tree数据结构,封装成函数
/** * 树形数据提取节点 * @param {*} data */ export function treeDataGetnode (data) { var res = [] var child= ...
- python中递归函数
python中的 递归函数,是指的是函数在函数内部调用自己的函数 需要满足两个条件,一,需要有一个明确的终止条件 二,需要函数自己在内部调用自己
- webform将一个usercontrol作为模态框在page上弹出
弹窗 public static void RegisterJQueryDialogScript(Page page, string dialogDivId, string title, int wi ...
- Php安装时出现的问题处理
问题从这里开始,我们一步一步说明: cd /usr/local/src/ tar zxvf php-5.5.6.tar.gz cd php-5.5.6 ./configure \ //执行当前目录下软 ...
- note《JavaScript 秘密花园》
点我跳转 (一)JavaScript-Garden-Object (二)JavaScript-Garden-Function (三)JavaScript-Garden-Array (四)JavaScr ...
- 斯坦福【概率与统计】课程笔记(四):EDA | 茎叶图
茎叶图的只做方法如下: 将每个数字分成茎和叶 对所有茎排序,并纵向从小到大放置好 对相同茎下的叶归到一起并排序,垂直于茎的排列方向放置好 举个例子:我们有一份奥斯卡影后的年龄集合: 34 34 27 ...
- IIS 解决跨域问题
打开 HTTP响应标头 添加如下三条 名称Access-Control-Allow-Origin 值*名称Access-Control-Allow-Headers 值Content-Type,Ac ...
- Dubbo 系列(05-1)服务发布
目录 Dubbo 系列(05-1)服务发布 Spring Cloud Alibaba 系列目录 - Dubbo 篇 1. 背景介绍 1.1 服务暴露整体机制 2. 源码分析 2.1 前置工作 2.2 ...