[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 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 <= 100000 <= A[i] <= 100000 <= K <= 10000
这道题是之前那道 [Smallest Range I](https://www.cnblogs.com/grandyang/p/11318240.html) 的拓展,那道题说的是每个数字可以加上 [-K, K] 范围内的任意一个数字,这道题说是每个数字必须加上K或者 —K,都是问新数组的最大值最小值之间的差值最小是多少。这两个条件有显著的区别,对于前一道题来说,非常的 flexible,因为可以加上 [-K, K] 范围内的任意一个数字,就是说也可以加上0,从而数字保持不变,那么只需要对原数组的最大值最小值进行修改即可,而这道题所有数字都强制要进行修改,则只考虑最大值最小值显然是不对的。来想想为什么不能直接对最小值加K,最大值减K,然后算差值。来看题目中的例子3,A = [1,3,6], K = 3,对最小值加K,得到4,对最大值减K,得到3,相减得到 -1,但实际上是不对的,因为中间的3也要进行加减操作,只能变成0或6,这样相当于改变了新数组的最大值最小值,最终算下来的结果应该是3。博主其实也尝试了暴力搜索法,即将原数组中的每个数字进行加K和减K,把得到的两个新数字放到一个新数组中,最后遍历新数组,找出最大值最小值并做差,结果超时了 Time Limit Exceeded!即便这只是一道 Medium 的题目,仍然不许我们用如此 Naive 的方法。只能另辟蹊径了。
如果不考虑数字修改,那么原数组的最大值最小值之间的差值就是所求,这里可以当作结果 res 的初始值。由于每个数字都需要加K或者减K,为了使得新数组的最大值最小值的差值最小,应该尽量使原数组中的较小的数字加K,较大的数字减K,所以最好是先给原数组排个序,然后在数组的某个位置i为界限,将原数组分为两段,前面所有的数字都加K,后面所有的数字都减K。则前半段 [0, i] 中的最大值是 A[i]+K,最小值是 A[0]+K,后半段 [i+1, n-1] 范围内的最大值是 A[n-1]-K,最小值是 A[i+1]-K,所以整个数组的最大值是 A[i]+K 和 A[n-1]-K 中的较大值,最小值是 A[0]+K 和 A[i+1]-K 中的较小值,二者做差就是可能的结果了,遍历所有的i,用每次计算出的差值来更新结果 res 即可,参见代码如下:
```
class Solution {
public:
int smallestRangeII(vector& A, int K) {
sort(A.begin(), A.end());
int n = A.size(), res = A[n - 1] - A[0];
int left = A[0] + K, right = A[n - 1] - K;
for (int i = 0; i
Github 同步地址:
https://github.com/grandyang/leetcode/issues/910
类似题目:
参考资料:
https://leetcode.com/problems/smallest-range-ii/
https://leetcode.com/problems/smallest-range-ii/discuss/173377/C%2B%2BJavaPython-Add-0-or-2-*-K
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 910. Smallest Range II 最小区间之二的更多相关文章
- [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 910. Smallest Range II
很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k ...
- 【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] 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 ...
- [LeetCode] 483. Smallest Good Base 最小的好基数
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- 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 ...
随机推荐
- 记录几篇PM文章
今日阅读了几篇关于 PM 的文章感觉还不错,整理.摘录于此: 1.[项目经理和产品经理:https://www.cnblogs.com/joylee/p/3541141.html] ——关于二者的异同 ...
- 关于Pragma
/** This is a introduction of how to use pragma. */ #pragma once /// This is used for include the he ...
- MySQL锁表查询SQL
// 查看进程 SHOW PROCESSLIST; // 查看是否锁表 SHOW OPEN TABLES WHERE In_use > 0; // 查看正在锁的事务 SELECT * FROM ...
- MongoDB自学------(3)MongoDB文档操作
一.插入文档 二.查询文档 三.更新文档 可以看到标题(title)由原来的 "Mongodb" 更新为了 "MongoDBtest". 以上语句只会修改第一条 ...
- 分布式 master/slave 框架
https://helix.apache.org/ https://stackoverflow.com/questions/16401412/apache-helix-vs-yarn https:// ...
- Microsoft.Office.Interop.Excel 读取 excel 中的 checkbox 和 radio
using Excel = Microsoft.Office.Interop.Excel; Excel.Application excelapp = new Excel.Application(); ...
- Java多线程——ThreadLocal类的原理和使用
Java多线程——ThreadLocal类的原理和使用 摘要:本文主要学习了ThreadLocal类的原理和使用. 概述 是什么 ThreadLocal可以用来维护一个变量,提供了一个ThreadLo ...
- Python基础18
“为什么有列表,还要元组?” 1. 元组可看成是简单的对象组合,而列表是随时间改变的数据集合. 2. 元组的不可变特性提供了某种完整性,确保元组不会被另一个引用来修改.类似于其它语言中的常数声明.
- Winform中使用FastReport的PictureObject时通过代码设置图片源并使Image图片旋转90度
场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- deepin把vscode设为默认文本应用
一开始我想自己写一个desktop文件放在/usr/share/applications下面,结果在右键菜单里面找不到vscode. [Desktop Entry] Categories=Develo ...