【LeetCode】910. Smallest Range II 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/smallest-range-ii/description/
题目描述
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
题目大意
可以把一个数组的每个数字加上K或者减去K,求每个位置都做了这个操作之后,最后的数组的最大值和最小值的差的最小值。
解题方法
把一个数组的每个数字加上K或者减去K然后求最大值最小值的差,等价于,把一个数组的每个数字加上2×K或者不变然后求最大值最小值的差。
我们先把数组进行排序,然后把每一个位置都做加上2×k的操作,同时保存每个位置进行操作后,整个数组的最大值和最小值。容易得出:
最大值是A[i] + 2 * K和A[-1]之一;
最小值是A[i + 1]和A[0] + 2 * K之一;
所以遍历一遍,我们就得出了最后的结果。
Python代码如下:
class Solution(object):
def smallestRangeII(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
A.sort()
N = len(A)
mn, mx = A[0], A[-1]
res = mx - mn
for i in range(N - 1):
mx = max(A[i] + 2 * K, mx)
mn = min(A[i + 1], A[0] + 2 * K)
res = min(mx - mn, res)
return res
C++代码如下:
class Solution {
public:
int smallestRangeII(vector<int>& A, int K) {
sort(A.begin(), A.end());
const int N = A.size();
int mn = A[0], mx = A[N - 1];
int res = mx - mn;
for (int i = 0; i < N - 1; i ++) {
mx = max(mx, A[i] + 2 * K);
mn = min(A[i + 1], A[0] + 2 * K);
res = min(res, mx - mn);
}
return res;
}
};
日期
2018 年 12 月 14 日 —— 12月过半,2019就要开始
【LeetCode】910. Smallest Range II 解题报告(Python & C++)的更多相关文章
- [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 910. Smallest Range II
很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】908. Smallest Range I 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【LeetCode】52. N-Queens II 解题报告(Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 全排列函数 回溯法 日期 题目地址:https:// ...
- 【LeetCode】454. 4Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
随机推荐
- R语言与医学统计图形-【20】ggplot2图例
ggplot2绘图系统--图例:guide函数.标度函数.overrides.aes参数 图例调整函数guide_legend也属于标度函数,但不能单独作为对象使用,即不能如p+guide_legen ...
- MAC——解决问题:打不开,因为它来自身份不明的开发者
今天在mac电脑上,下载了一个软件,是从某个网页上下载的,点击却不能打开,弹出窗口提示说"打不开xx,因为它来自身份不明的开发者",怎么解决?下面来看下. 方法/步骤 点击 ...
- Oracle-连接多个字段
0.函数concat(A,B)作用:链接字符串 区别: Oracle中:CONCAT()只允许两个参数:(貌似可以内嵌) Mysql中:CONCAT()可以连接多个参数: 1.用||符号进行拼接 例子 ...
- Vue 前端配置多级目录实践(基于Nginx配置方式)
前情提要 有阵子没更新博客了,因为快年结了工作比较多,这不,最近公司的对外演示环境出现问题这个活儿也落到了我的头上-- 事情是这样的,原来演示环境有很多服务,每个服务都是对外单独开一个端口,比如 ht ...
- abuse
abuse 近/反义词: ill-treat, maltreat, mistreat, misuse, prostitute, spoil; defame, disparage, malign, re ...
- ache
ache和pain可能没啥差别,头疼和头好痛都对.从词典来看,有backache, bellyache, earache, headache, heartache, moustache/mustach ...
- Dos窗口下中文乱码问题
最近用Datax工具进行数据同步时,在DOS窗口下出现了中文乱码问题,导致一些错误只能到Log中查看,在网上找了一些方法,记录使用成功的方法. Dos命令:chcp 通过cmd进入Dos命令窗口,执行 ...
- Linux shell实现每天定时备份mysql数据库
每天定时备份mysql数据库任务,删除指定天数前的数据,保留指定天的数据: 需求: 1,每天4点备份mysql数据: 2,为节省空间,删除超过3个月的所有备份数据: 3,删除超过7天的备份数据,保留3 ...
- springmvc中拦截器的定义和配置
package com.hope.interceptor;import org.springframework.lang.Nullable;import org.springframework.web ...
- ssm动态查询向前台传json
1.数据协议层 public User selectById(Integer id);//通过id值查询用户 2.数据层 <select id="selectById" re ...