LeetCode 910. Smallest Range II
很有意思的一道数学推理题目, 剪枝以后解法也很简洁。
初看貌似需要把每个数跟其他数作比较。但排序以后可以发现情况大大简化:
对于任一对元素a[i] < a[j], a[i] - k和a[j] + k 的情况可以排除, 因为会产生比原值更大的差, 所以对于原有数组的最小值min最大值max, (min - k, max + k)的情况可以排除。剩下的三种情况, (min - k, max - k), (min + k, max + k) 和 (min + k, max - k),后两种等价于原值max - min, 我们可以把初始最大差值设为max - min, 确保最终结果不会比这个平凡值更坏。
对于最后一种情况(min + k, max - k), 需要继续分情况讨论。
方便起见,我们可以把所有元素都预先-k, 然后从最小元素开始,尝试依次把各元素+2*k.
我们可以证明,如果选择a[i] + 2 * k,那么之前任一元素a[j] (0 <= j < i)加上 2 * k,都不会使结果更坏。证明见此:
B = [A[0] + 2K, ...,A[i-1]+2K, A[i] + 0, A[i+1] +2K, ..., A[j]+2K, ..., A[j+1]+0, A[n-1]+0]
B' = [A[0] + 2K, ...,A[i-1]+2K, A[i] + 2K, A[i+1] +2K, ..., A[j]+2K, ..., A[j+1]+0, A[n-1]+0]
A[i]+2K is between A[i-1] + 2K and A[i+1] +2K, so it must stand in the range of B.
B' is not worse than B, it can be easily generalized to multiple elements added by 0 between the ones added by 2K.
于是当我们考虑a[i] + 2 * k时,可以假设之前的元素都已经加上了2 * k。
当前最大差值取决于:
1) 当前数列最小值:
可能为a[i+1]
a[i+1],...a[0]+2*k...a[n-1]...
或a[0]+2*k
a[0]+2*k..., a[i+1],...a[n-1]...
2) 以及当前数列最大值:
可能为a[i]+2*k
...a[n-1]...a[i]+2*k
或者a[n-1]
...a[i]+2*k...a[n-1]
因此我们只需遍历所有元素,计算当前元素加上2*k的以后的数组的最大差值,取其中的最小值即可。
public:
int smallestRangeII(vector<int>& a, int k) {
size_t len = a.size();
if(len <= ) return ;
sort(a.begin(), a.end());
int front = a.front(), back = a.back(), d = back - front, start = front + * k;
if( k >= d) return d;
int ans = d;
for(size_t i = ; i < len - ; i++){
int lo = min(start, a[i + ]), hi = max(a[i] + * k, back);
ans = min(ans, hi - lo);
}
return ans;
}
参考:https://zhanghuimeng.github.io/post/leetcode-910-smallest-range-ii/
LeetCode 910. Smallest Range II的更多相关文章
- [LeetCode] 910. Smallest Range II 最小区间之二
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...
- 【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 > ...
- 910. Smallest Range II
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...
- [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] 632. Smallest Range Covering Elements from K Lists 覆盖K个列表元素的最小区间
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- [leetcode]632. Smallest Range最小范围
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- [Swift]LeetCode910. 最小差值 II | Smallest Range II
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...
- 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 ...
随机推荐
- MySQL 数据库--内置功能
一 视图 视图:是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 优点:们可以把查询过程中的临 ...
- Asp.net core 项目实战 新闻网站+后台 源码、设计原理 、视频教程
首先说明,视频教程.源码并非本人原创 本人将项目分割开,并写了一些说明. 该视频教程 地址 https://study.163.com/course/courseMain.htm?courseId= ...
- 申请MVP奖励时的小Tips
大家新年好,今天MSPrecious为大家带来一些申请MVP奖励时的小Tips. 本文分为三个部分 MVP是什么 如何申请MVP 申请MVP需要注意的事项 MVP是什么? 我想,点进来看这篇文章的 ...
- December 07th 2016 Week 50th Wednesday
Life is a flower, and love is the honey of the flower. 人生是花儿,而爱情就是花的蜜. My life is not as beautiful a ...
- Apache,php配置
很多朋友想要在window下配置apache+php+mysql运行环境,其实从这么长时间的经验来看,win2003下还是用iis,如果为了学习或对apache特别熟悉的朋友可以用apache 一 ...
- 43、ThreadPool 、WaitHandle、原子操作InterLocked
ThreadPool 创建线程需要时间.如果有不同的小任务要完成,就可以事先创建许多线程/在应完成这些任务时发出请求.不需要自己创建这样一个列表. 该列表由ThreadPool类托管.这个类会在需要时 ...
- jq实现简单的滑动解锁效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- JavaScript浏览器对象模型(BOM)之history对象
history 对象是 window 对象的属性,它保存着用户上网的记录,从窗口被打开的那一刻算起. 一.history对象的属性 可以通过判断 history.length,得到是否有历史记录和记录 ...
- SOJ 1017 Power of Cryptography 库函数精度
Background Current work in cryptography involves (among other things) large prime numbers and comput ...
- SOJ 1089 FatMouse的奶酪 暴力
BackGround FatMouse拥有许多奶酪,所有的奶酪都有一个彼此不同的重量.FatMouse为了控制它自己的体重,在每天早上它都会计算今天应该吃多少奶酪才能无痛苦地减肥. The Probl ...