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 add x
to A[i] (only once)
.
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: 3
Explanation: B = [4,6,3]
Note:
1 <= A.length <= 10000
0 <= A[i] <= 10000
0 <= K <= 10000
Approach #1: C++.
class Solution {
public:
int smallestRangeII(vector<int>& A, int K) {
int N = A.size();
sort(A.begin(), A.end());
int ans = A[N-1] - A[0]; for (int i = 0; i < A.size()-1; ++i) {
int a = A[i], b = A[i+1];
int high = max(A[N-1]-K, a+K);
int low = min(A[0]+K, b-K);
ans = min(ans, high-low);
} return ans;
}
};
Analysis:
We can formalize the above concept: if A[i] < A[j]
, we don't need to consider when A[i]
goes down while A[j]
goes up. This is because the interval (A[i] + K, A[j] - K)
is a subset of (A[i] - K, A[j] + K)
(here, (a, b)
for a > b
denotes (b, a)
instead.)
That means that it is never worse to choose (up, down)
instead of (down, up)
. We can prove this claim that one interval is a subset of another, by showing both A[i] + K
and A[j] - K
are between A[i] - K
and A[j] + K
.
For sorted A
, say A[i]
is the largest i
that goes up. Then A[0] + K, A[i] + K, A[i+1] - K, A[A.length - 1] - K
are the only relevant values for calculating the answer: every other value is between one of these extremal values.
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[j], a[i] - k和a[j] + k ...
- 【leetcode】910. Smallest Range II
题目如下: 解题思路:我的思路是先找出最大值.对于数组中任意一个元素A[i]来说,如果A[i] + K 是B中的最大值,那么意味着从A[i+1]开始的元素都要减去K,即如果有A[i] + K > ...
- [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 ...
- Smallest Range II
2020-01-21 21:43:52 问题描述: 问题求解: 这个题目还是有点难度的,感觉很巧妙也很难想到. 整体的思路如下: 1. 首先原问题等价于 +0 / + 2*K 2. 那么res = M ...
- [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] Smallest Range 最小的范围
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- [Swift]LeetCode632. 最小区间 | Smallest Range
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
随机推荐
- 转载--浅谈spring4泛型依赖注入
转载自某SDN-4O4NotFound Spring 4.0版本中更新了很多新功能,其中比较重要的一个就是对带泛型的Bean进行依赖注入的支持.Spring4的这个改动使得代码可以利用泛型进行进一步的 ...
- 从0开始用spring boot编写分布式配置中心-peppa
欢迎大家一起来编写peppa github地址: github 交流群: 目前市面上比较流行的分布式配置中心有disconf.apollo,用起来还是比较方便的,然而由于在权限管理这块做得不够好,导致 ...
- std::mutex 引起的 C2280 尝试引用已删除的函数
起因是把之前写的类中的 mutex 使用了(之前注释掉了没用到这个变量); 或者说添加了一个 mutex 变量, 然后 这个类有嵌套在了 其类的 map 中使用, 然后 编译 就报错 ` C2280 ...
- HowTo: Xen 4.1.3 Windows 8 HVM domU with Intel HD4000 VGA Passthrough on Debian Wheezy
http://linux-bsd-sharing.blogspot.com/2012/10/howto-xen-413-windows-8-hvm-domu-with.html Update 05/0 ...
- code1540 银河英雄传说
pa[i]代表i的father pre[i]代表i之前有多少个 sum[i]代表i所在的整列有多少个 cc为命令类型,x y为命令参数, fx fy分别为x y的father 当cc==‘M’时,合并 ...
- cakephp绑定事件
<input type='button' id="submitBtn" class="col button button-fill " value='确认 ...
- 白盒测试实践项目(day5)
在这几天的工作下,小组成员都基本完成了各自所负责的内容. 李建文同学完成提交了代码复审相关文档后,也经过小组的补充,彻底完成. 汪鸿同学使用FIndBugs工具完成了静态代码的测试,并且也完成了静态代 ...
- Firefox 43无法安装xpi的问题
Firefox 43无法安装xpi的问题 说明:Firefox 42将默认禁止安装未签名扩展 强制禁用这个首选项(高级用户): 你可以在 Firefox 配置编辑页面 (about:config ...
- Java概述、环境变量、注释、关键字、标识符、常量
Java语言的特点 有很多小特点,重点有两个开源,跨平台 Java语言是跨平台的 Java语言的平台 JavaSE JavaME--Android ...
- MFC中按钮控件的用法笔记(转)
VC学习笔记1:按钮的使能与禁止 用ClassWizard的Member Variables为按钮定义变量,如:m_Button1:则m_Button1.EnableWindow(true); 使按钮 ...