[LeetCode] 908. Smallest Range I 最小区间
Given an array `A` of integers, for each integer `A[i]` we may choose any `x` with `-K 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: 0 Explanation: B = [3,3,3] or B = [4,4,4]
Note:
1 <= A.length <= 10000
0 <= A[i] <= 10000
0 <= K <= 10000
这道题说是给了一个非负数的数组,和一个非负数K,说是数组中的每一个数字都可以加上 [-K, K] 范围内的任意一个数字,问新数组的最大值最小值之间的差值最小是多少。这道题的难度是 Easy,理论上应该是可以无脑写代码的,但其实很容易想的特别复杂。本题的解题标签是 Math,这种类型的题目基本上就是一种脑筋急转弯的题目,有时候一根筋转不过来就怎么也做不出来。首先来想,既然是要求新数组的最大值和最小值之间的关系,那么肯定是跟原数组的最大值最小值有着某种联系,原数组的最大值最小值我们可以很容易的得到,只要找出了跟新数组之间的联系,问题就能迎刃而解了。题目中说了每个数字都可以加上 [-K, K] 范围内的数字,当然最大值最小值也可以,如何让二者之间的差值最小呢?当然是让最大值尽可能变小,最小值尽可能变大了,所以最大值 mx 要加上 -K,而最小值 mn 要加上K,然后再做减法,即 (mx-K)-(mn+K) = mx-mn+2K,这就是要求的答案啦,参见代码如下:
解法一:
class Solution {
public:
int smallestRangeI(vector<int>& A, int K) {
int mx = A[0], mn = A[0];
for (int num : A) {
mx = max(mx, num);
mn = min(mn, num);
}
return max(0, mx - mn - 2 * K);
}
};
我们也可以使用 STL 自带的求最大值最小值的函数,从而一行搞定碉堡了~
解法二:
class Solution {
public:
int smallestRangeI(vector<int>& A, int K) {
return max(0, *max_element(A.begin(), A.end()) - K - (*min_element(A.begin(), A.end()) + K));
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/908
类似题目:
参考资料:
https://leetcode.com/problems/smallest-range-i/
https://leetcode.com/problems/smallest-range-i/discuss/173512/C%2B%2B-1-liner
https://leetcode.com/problems/smallest-range-i/discuss/173367/C%2B%2BJavaPython-Check-Max-Min
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 908. Smallest Range I 最小区间的更多相关文章
- [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 908 Smallest Range I 解题报告
题目要求 Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...
- 【Leetcode_easy】908. Smallest Range I
problem 908. Smallest Range I solution: class Solution { public: int smallestRangeI(vector<int> ...
- [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】908. Smallest Range I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- [LeetCode&Python] Problem 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】908. Smallest Range I
题目如下: 解题思路:简单的不能再简单的题目了,对于任意一个A[i]来说,其可能的最小的最大值是A[i]-K,最大的最小值是A[i]+K.遍历数组,求出所有元素中最大的最小值和最小的最大值,两者之差( ...
- [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 ...
随机推荐
- 解决移动端ios下overflow-x scroll无法隐藏滚动条的问题
这次有个需求是在web首页添加分类菜单,一共是8个分类,在移动端水平展示,可以左右滚动. 最后在手机上微信浏览器看到是有个滚动条,非常影响美观. 主要通过以下代码实现水平滚动 white-space: ...
- WPF 开源框架项目介绍
旧版本项目说明 旧版本由于是从学习WPF进行开发的, 历经时长有半年之余,基本上现学现用的那种, 所以存在很多缺陷, 由于整体的设计多处更新, 故旧版本将会终止维护(砍), 基于WCF的项目也会停止, ...
- PhaseScorer:感慨高手写的代码就是精炼
看懂了PhaseScorer的算法后,回想起前面看的算法和代码,感慨高手写的代码总是那么精炼,没有一句废话,多一句不行,少一句不行.明天来了写下PhaseScorer算法的实现:todo
- C#关键字 const与readonly
====const==== const关键字来声明某个常量字段或常量局部变量.常量字段和常量局部变量不是变量而且不能修改.常量可以为数字.布尔值.字符串或null引用. 常数声明的类型指定声明引入的成 ...
- Vue 动态修改data 值 并触发视图更新
Vue 动态修改data 值 并触发视图更新 this.$set(obj, key, '') // Vue 动态修改或者添加data key 并触发视图更新
- 2019-07-23 类的继承和final关键字的应用
我们称以存在的用来派生新类的类为基类,又称做父类,超类.由已存在的类派生出的新类称为派生类,又称为子类.从一个基类派生的继承称单继承,从多个基类派生的继承称为多继承.也就是说:一个类只能直接从一个类中 ...
- 英语Lignaloes沉香木LIGNALOES单词
中文名沉香木 外文名Lignaloes 国内分布两广以及云南和福建等地 国外分布印度尼西亚.马来西亚.新加坡 沉香木是珍贵的香料,被用作燃烧熏香.提取香料.加入酒中,或直接雕刻成装饰品.沉香木又名沉水 ...
- 通过MES技术居然可以防止制造数据造假?
近些年来我们经历了太多制造数据造假事件,特别是前段时间曝出的医药制造事件更是将我们群众的愤怒值推到了最高点.不过我们最应当做的是,冷静下来,思考一下各行各业的我们是不是都该做些什么了?毕竟当下一个灾难 ...
- authenticating with the app store 一直卡住--问题记录
参考链接:https://blog.csdn.net/csdn2314/article/details/90021367 authenticating with the app store 一直卡住最 ...
- [PHP] 广度优先搜索匹配网站所有链接
<?php define('PRE_DOMAIN','www'); define('DOMAIN','sina.com.cn'); define('PROTOCOL','https'); def ...