解题报告-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 ...
随机推荐
- 总线设备驱动模型---platform篇
总线设备驱动模型----驱动篇 http://blog.chinaunix.net/uid-27664726-id-3334923.html http://blog.chinaunix.net/uid ...
- 走在linux 的路上
终于现在不看鸟哥的私房菜基础篇了,以后再慢慢看,像我这种初学者,感觉还是不太适合看鸟哥的私房菜. 于是从图书馆借了本书继续学习我的linux. 这样看着linux容易多了,进而熟悉了几个命令:ls c ...
- 每天一个linux命令(网络):【转载】route命令
Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...
- 关于oracle的profiles
一.关于oracle的profiles profiles文件是口令和资源限制的配置集合,包括CPU的时间.I/O的使用.空闲时间.连接时间.并发会话数量.密码策略等对于资源的使用profile可以做到 ...
- 关于mybatis中一级缓存和二级缓存的简单介绍
关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...
- ThinkPHP think-migration 简洁使用教程
ThinkPHP think-migration 简洁使用教程 migration:一种数据库的版本控制,让团队在修改数据库结构的同时,保持彼此的进度一致.帮你更简单的管理数据库.基于原生 think ...
- [CLPR] 用于加速训练神经网络的二阶方法
本文翻译自: http://www.codeproject.com/Articles/16650/Neural-Network-for-Recognition-of-Handwritten-Digi ...
- 【python】使用Python中的urlparse、urllib抓取和解析网页
一.解析URL 函数urlparse(urlstring [, default_scheme [, allow_fragments]])的作用是将URL分解成不同的组成部分,它从urlstring中取 ...
- [转]Docker 为什么这么火
本文来自:Docker为什么这么火 以及此文:http://cloud.51cto.com/art/201408/447966_1.htm Docker 我的理解是,相对于 VMware 的一个分支. ...
- Hive组件以及执行过程
对Hive的基本组成进行了总结: 1.组件: 元存储(Metastore )-存储“系统目录以及关于表.列.分区等的元数据”的组件.驱动(Driver )- 控制 HiveQL 生命周期的组件,当 H ...