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的更多相关文章

  1. [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 ...

  2. 【LeetCode】910. Smallest Range II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. [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 ...

  4. LeetCode 910. Smallest Range II

    很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k ...

  5. 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 ...

  6. 【leetcode】910. Smallest Range II

    题目如下: 解题思路:我的思路是先找出最大值.对于数组中任意一个元素A[i]来说,如果A[i] + K 是B中的最大值,那么意味着从A[i+1]开始的元素都要减去K,即如果有A[i] + K > ...

  7. [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 ...

  8. [LeetCode] Smallest Range 最小的范围

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  9. [Swift]LeetCode632. 最小区间 | Smallest Range

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

随机推荐

  1. Centos7.X 搭建Prometheus+node_exporter+Grafana实时监控平台

    Prometheus简介 什么是 Prometheus Prometheus是一个开源监控报警系统和时序列数据库 主要功能 多维数据模型(时序由 metric 名字和 k/v 的 labels 构成) ...

  2. selenium+requests进行cookies保存读取操作

    看这篇文章之前大家可以先看下我的上一篇文章:cookies详解 本篇我们就针对上一篇来说一下cookies的基本应用 使用selenium模拟登陆百度 from selenium import web ...

  3. C:数组习题

    与字符串处理有关的函数: 头文件:<stdio.h>    gets().puts() 头文件:<string.h> (1).字符串长度测量函数  :strlen(字符数组名) ...

  4. Java入门教程九(封装继承多态)

    封装 封装就是将对象的属性和方法相结合,通过方法将对象的属性和实现细节保护起来,实现对象的属性隐藏.做法就是:修改属性的可见性来限制对属性的访问,并为每个属性创建一对取值(getter)方法和赋值(s ...

  5. VUE中登录密码显示与隐藏的最简设计——基于iview

    目录 VUE中登录密码显示与隐藏的最简设计--基于iview 1.背景 2.实现最终效果 2.1 隐藏密码 2.2 显示密码 3.实现思路 3.1 v-if判断当前密码显示状态 3.2 密码隐藏状态 ...

  6. C语言入门理解指针

    本文章为本人原创,适合于刚入坑C语言,对于指针的定义和用法模糊不清的同学,如有不正,请各位指出. 从根本来说,指针变量也是变量,只是int变成了int *,以此类推.只不过指针变量里面放的内容是普通变 ...

  7. 石油测井专题(六)MCM工艺在LWD的应用

    在上一篇的MCM工艺我们提到过石英挠性加速度计的伺服电路采用此工艺可以有效提高仪器产品的稳定性和寿命. MCM相对于印制电路板(PCB)来讲,MCM技术采用了更短的连接长度和更紧密的器件布局,从而降低 ...

  8. 前端基础知识之HTML

    [1: What does a doctype do?] 1: doctype是html文件的第一行代码,意味着它的前面有注释都不行.所以要要写在<html>标签前面,而且它不属于html ...

  9. Python学习笔记--迭代

    在Python中,迭代是通过for ... in来实现.只要是可迭代的对象都可以用for ... in来进行历遍. 常用的有list.tuple.dict等.举例如下: 列表的迭代: L=[1,2,3 ...

  10. 基于Unix Socket的可靠Node.js HTTP代理实现(支持WebSocket协议)

    实现代理服务,最常见的便是代理服务器代理相应的协议体请求源站,并将响应从源站转发给客户端.而在本文的场景中,代理服务及源服务采用相同技术栈(Node.js),源服务是由代理服务fork出的业务服务(如 ...