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 ...
随机推荐
- December 25th 2016 Week 53rd Sunday
Patience is bitter, but its fruit is sweet. 忍耐是痛苦的,但它的果实是甜蜜的. What can we do if there is no fruit of ...
- 自动出题判分——c#学习实践
1.程序功能自动出题—(程序随意给出10以内加减乘除)并且判断结果正确与否,正确打对勾,错误打错号. 2.设计界面: 3.代码: using System; using System.Collecti ...
- 微软Charting图表控件 System.Web.UI.DataVisuliztion.Charting
一.概述 基于.NET Framework 3.5 SP1的图表控件--Chart,可在WinForm和WebForm下使用!需要引入System.Web.DataVisualization.dll ...
- 关于 MapReduce
继续摘抄<Hadoop 权威指南>第二章,跳过不少于我复杂的东西,但依然是捉急的效率,开始觉得看不完另外一本全英文的书,大概每天要看5页吧... 以上. MapReduce 是一种可用于数 ...
- Debian9+PHP7+MySQL+Apache2配置Thinkphp运行环境LAMP
因工作需要,配置了一台服务器,运行THINKPHP框架程序,记录配置过程如下: 安装net版Debian9,完成后,如下: 1.配置基本的网络 php install net-tools 安装net- ...
- virtualbox+vagrant学习-5-Boxes-2-Box Versioning
Box Versioning 从Vagrant 1.5版本开始, box支持版本控制.这允许创建box的人将更新推送到box中,使用box的人有一个简单的工作流,用于检查更新.更新box以及查看发生了 ...
- topk两种解法
1.这个通过partition实现topk,时间复杂度是o(logn*logn),也就是0(n),但需要修改原数组的顺序 下面这个代码本身有一些错误,并且throw excption会在牛客上报错 c ...
- URL列表
MySql函数大全:http://www.cnblogs.com/xuyulin/p/5468102.html
- maven加速镜像
<mirror> <id>repo3</id> <mirrorOf>central</mirrorOf> <name>Human ...
- PAT——1030. 完美数列
给定一个正整数数列,和正整数p,设这个数列中的最大值是M,最小值是m,如果M <= m * p,则称这个数列是完美数列. 现在给定参数p和一些正整数,请你从中选择尽可能多的数构成一个完美数列. ...