Smallest Range II
2020-01-21 21:43:52
问题描述:


问题求解:
这个题目还是有点难度的,感觉很巧妙也很难想到。
整体的思路如下:
1. 首先原问题等价于 +0 / + 2*K
2. 那么res = Max - Min
3. 不断更新Max,Min期望得到更小的res
public int smallestRangeII(int[] A, int K) {
int n = A.length;
Arrays.sort(A);
int max = A[0];
int min = A[0];
for (int num : A) {
if (num > max) max = num;
if (num < min) min = num;
}
int res = max - min;
for (int i = 0; i < n - 1; i++) {
max = Math.max(A[n - 1], A[i] + 2 * K);
min = Math.min(A[0] + 2 * K, A[i + 1]);
res = Math.min(res, max - min);
}
return res;
}
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 ...
- [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 910. Smallest Range II
很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + 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】910. Smallest Range II
题目如下: 解题思路:我的思路是先找出最大值.对于数组中任意一个元素A[i]来说,如果A[i] + K 是B中的最大值,那么意味着从A[i+1]开始的元素都要减去K,即如果有A[i] + K > ...
- [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 ...
随机推荐
- Book. Effective C++ item2-尽量使用const, enum, inline替换#define
##常规变量 c++里面的#define后面的定义部分,是不算代码的一部分的.所以如果你使用#define: #define ASPECT_RATIO 1.653 你希望这个代号ASPECT RATI ...
- 初识SpringAOP
概述 AOP(Aspect Oriented Programming),即面向切面编程 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延伸,是软件系统开发中的一个 ...
- Sublime Text3 旧版本下载以及破解激活方式
前言 当前Sublime Text3 出到了32**版本,以前直接输入激活码的方法已经不能使用. 而官网又不提供旧版本的下载链接,因此在此分享旧版本下载方式以及激活方式. 下载方法 通过下面这个链接下 ...
- javascript简单轮播图
**轮播图实现原理: 通过多张图片平铺,用overflow:hidden只显示一张图片.其他的隐藏,无缝滚动用定时器改变元素的left值让图片呈现左右滚动的效果.** HTML布局和内容: 1.容器c ...
- Win32 按钮嵌套收不到消息解决记录
太长不看 SetWindowSubClass,然后 return DefSubclassProc(hWnd, uMsg, wParam, lParam);,不要有 WS_CHILD 这个 Style. ...
- Python知识点 - Xpath提取某个标签,需要转换为HTML。
# lxml转Html from lxml import etree from HTMLParser import HTMLParser def lxml_to_html(text:etree ...
- 细说集群技术(Cluster)
今天本人给大家讲解一些我对集群技术一个理解,如有不对的或者讲的不好的可以多多提出,我会进行相应的更改,先提前感谢提出意见的各位了!!! 集群(Cluster)技术:通过此可以用较低的成本获取较高的性能 ...
- SpringBoot1.5.10.RELEASE整合druid
1.先在pom文件中导入druid的jar包 <dependency> <groupId>com.alibaba</groupId> <artifactId& ...
- 基于springcloud搭建项目-Ribbon篇(三)
这篇文章主要是介绍一下ribbon的用法,我们都知道ribbon是负载均衡,但是却不知道他是怎么样的负载均衡,怎么用,能干嘛? ● 其实,简单的说,Spring Cloud Ribbon是基于Netf ...
- C# 简单地使用下 音频解码器Bass.Net
在C#中有许多音频播放的方案,例如WinForm里调用系统自带MediaPlayer的COM组件和WPF的MediaPlayer(实质上还是WindowsMediaPlayer) 以及一堆API播放和 ...