解题报告-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]
解题思路 :
因为题目希望 从所有数组B中,计算出所有最大值和最小值的差值,并从中取最小的差值
所以我们的目标是使数组B中的 最大值尽可能小,最小值尽可能大
第一步确定最大值里,最小的,因为max元素最多只能减小到 max - K, 所以最大值不可能小于 max - K
第二步确定最小值里,最大的,因为min元素最多只能增加到 min + K, 所以最小值不可能大于 min + K
比较max - K 和 min + K
如果max - K <= min + K, 其他元素的取值范围正好可以覆盖[max - K, min + K]的区间, 因为每一个元素a[i], a[i] + K >= min + K, a[i] - K <= max - K
说明所有元素可以取到相同值,则最大值和最小值差距可以为0
如果max - K > min + K, 则由于最小的最大值为max - K, 最大的最小值为min + K,
则差距为 max - min - 2 * K
c++代码
class Solution {
public:
//how to prove this process
//min min + k
//max max - k
//如果min + k >= max - k 其它元素的取值范围一定是可以覆盖min + k, max - k
//所以是最小值是0
//如果min + k < max - k 则最大和最小的差值 = max - min - 2 * k
int smallestRangeI(vector<int>& A, int K) {
//edge case
int minV = INT_MAX;
int maxV = INT_MIN;
for (int d : A) {
minV = min(d, minV);
maxV = max(d, maxV);
}
int ans = 0;
if (maxV - K <= minV + K) return ans;
return maxV - minV - 2 * K;
}
};
java代码
class Solution {
public int smallestRangeI(int[] A, int K) {
int minV = Integer.MAX_VALUE;
int maxV = Integer.MIN_VALUE;
int ans = 0;
for (int d : A) {
minV = Math.min(minV, d);
maxV = Math.max(maxV, d);
}
if (minV + K >= maxV - K) return ans;
return maxV - minV - 2 * K;
}
}
解题报告-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 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 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
题目如下: 解题思路:简单的不能再简单的题目了,对于任意一个A[i]来说,其可能的最小的最大值是A[i]-K,最大的最小值是A[i]+K.遍历数组,求出所有元素中最大的最小值和最小的最大值,两者之差( ...
- 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】632. Smallest Range 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
随机推荐
- 接口测试SoapUI参数化之Datasource20151002
上次和大家一起完成了soapui的参数之一properties,今天我们一起交流另外一种参数化的方法,跟着一起练习,不懂不要紧,练习多了就会慢慢懂的: 1.准备excle(目前soapui只支持xls ...
- mysql sql语句高级写法
将user表的内容,插入到team_member表INSERT INTO team_member (Nike,HeadImageUrl) SELECT Nike,HeadImageUrl FROM u ...
- 用pil产生验证码出现:ImportError: The _imagingft C module is not installed
这个是由于PIL没有编译freetype导致的查看 lib/python2.7/site-packages/PIL/看看 _imagingft.so 是否存在 # 需要先安装jpeg库wget htt ...
- php 递归读取目录
看到很多面试题有这个,今天有机会写了一下. 要注意的是: 在opendir这个函数用完后,要注意closedir,因为安全问题,打开的目录依然存在于内存中,在并发情况下最好关闭,不然容易被破坏. &l ...
- Codeforces 158B:Taxi
B. Taxi time limit per test 3 seconds memory limit per test 256 megabytes input standard input outpu ...
- zoj 1828 Fibonacci Numbers
A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the firs ...
- 在css中使用hover来控制其他元素的样式,该两个元素必须是父子元素
.col-3:hover .check-box { display: block; } 在css中使用hover来控制其他元素的样式,该两个元素必须是父子元素!!!!
- Android分类导航
新的一年,将在2017下半年所写的博文作了个总结,也是方便自己以后查阅,整理完也是发现在这半年而是学到了许多,新的一年,继续努力! 我的第一个Android开源库——CirclePointMove中文 ...
- highlight.js 设置行号
原文地址:highlight.js 设置行号 博客地址:http://www.extlight.com 一.背景 笔者在开发这套博客系统时使用 Editormd 作为 Markdown 编辑器,由于不 ...
- 在laravel之外使用eloquent
视频地址 https://laracasts.com/lessons/how-to-use-eloquent-outside-of-laravel