908. Smallest Range I
Given an array
Aof integers, for each integerA[i]we may choose anyxwith-K <= x <= K, and addxtoA[i].After this process, we have some array
B.Return the smallest possible difference between the maximum value of
Band the minimum value ofB.
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 <= 100000 <= A[i] <= 100000 <= K <= 10000
Approach #1: Math. [Java]
class Solution {
public int smallestRangeI(int[] A, int K) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < A.length; ++i) {
if (min > A[i]) min = A[i];
if (max < A[i]) max = A[i];
}
if (max - min < 2 * K) return 0;
return max - min - 2 * K;
}
}
Analysis:
If you can find that the result only relate with (max - min) and 2 * K, it will become easy to solve.
908. Smallest Range I的更多相关文章
- 【Leetcode_easy】908. Smallest Range I
problem 908. Smallest Range I solution: class Solution { public: int smallestRangeI(vector<int> ...
- [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 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&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 ...
- 解题报告-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】908. Smallest Range I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- 【leetcode】908. Smallest Range I
题目如下: 解题思路:简单的不能再简单的题目了,对于任意一个A[i]来说,其可能的最小的最大值是A[i]-K,最大的最小值是A[i]+K.遍历数组,求出所有元素中最大的最小值和最小的最大值,两者之差( ...
- [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 ...
随机推荐
- 正则表达式匹配${key}并在Java中使用
1.正则表达式匹配${key} \$\{([a-z]+)\} 能够匹配字符串中以${key}形式的文本(其中key为小写应为字母) .*\$\{([a-z]+)\}.* 可以用来检测文本中是否有${k ...
- java拼接JSON串
String str = "{\"route\":\"onGift\",\"time\":\"\",\&quo ...
- POJ-3080(KMP+多个字符串的最长公共子串)
Blue Jeans HDOJ-3080 本题使用的是KMP算法加暴力解决 首先枚举第一个字符串的所有子串,复杂度为O(60*60),随后再将每个子串和所有剩下的m-1个字符串比较,看是否存在这个子串 ...
- System.Net.Mail邮件发送抄送附件(多个)
/// <summary> /// 邮件发送抄送附件 /// </summary> /// <param name="mailTo">收件人(可 ...
- CentOS 7关闭firewalld启用iptables 开放端口
在CentOS7中,有很多CentOS 6中的常用服务发生了变化. 其中iptables是其中比较大的一个.防火墙iptables被firewalld取代. 本文将介绍,如果采用systemctl关闭 ...
- mysql 基本指令 1
desc 表名 --查看表属性 show create table 表名 \g; --查看代码 alter table 表名 auto_increment=20; --改自增的值 MySQL:自 ...
- 选择 FreeBSD 而不是 Linux 的技术性原因3
选择 FreeBSD 而不是 Linux 的技术性原因3 jail FreeBSD Jails 系统是另一个惊人的工程壮举. 在 2000 年 3 月 14 日的 4.0 版本中,FreeBSD 引入 ...
- ajax轮询原理及其实现方式
ajax轮询原理及其实现方式 ajax轮询的两种方式 方式1:设定一个定时器,无论有无结果返回,时间一到就会继续发起请求,这种轮询耗费资源,也不一定能得到想要的数据,这样的轮询是不推荐的. 方式2: ...
- Mysql将查询结果某个字段以逗号分隔,使用group_concat函数可以实现(配合group by使用)
示例:SELECT Id, GROUP_CONCAT(Name SEPARATOR ',') Names FROM some_table GROUP BY id
- P4285 [SHOI2008]汉诺塔 题解 (乱搞)
题目链接 P4285 [SHOI2008]汉诺塔 解题思路 提供一种打表新思路 先来证明一个其他题解都没有证明的结论:\(ans[i]\)是可由\(ans[i-1]\)线性递推的. (\(ans[i] ...