Smallest Range II

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;
    }

  

原文地址:https://www.cnblogs.com/hyserendipity/p/12227140.html

时间: 2024-08-30 14:03:23

Smallest Range II的相关文章

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

LeetCode 910. Smallest Range II

很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k 的情况可以排除, 因为会产生比原值更大的差, 所以对于原有数组的最小值min最大值max, (min - k, max + k)的情况可以排除.剩下的三种情况, (min - k, max - k), (min + k, max + k) 和 (min + k, max - k),后两种等价于原值max

一道题目- Find the smallest range that includes at least one number from each of the k lists

You have k lists of sorted integers. Find the smallest range that includes at least one number from each of the k lists. For example, List 1: [4, 10, 15, 24, 26] List 2: [0, 9, 12, 20] List 3: [5, 18, 22, 30] The smallest range here would be [20, 24]

[LeetCode] 632. Smallest Range Covering Elements from K Lists

[LeetCode]632. Smallest Range Covering Elements from K Lists 你有 k 个升序排列的整数数组.找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中. 我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b] 比 [c,d] 小. 示例 1: 输入:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] 输出: [20,24] 解释: 列表 1:

lc 632. Smallest Range

https://leetcode.com/problems/smallest-range/description/ 给你k个数组,找一个最小区间[a,b],可以包含k个数组中的数字各至少一个. 滑动窗口题. 对于要求"最短"的题目很适用. points: 1.在扩张右界的时候,一旦碰到合法就停止,但不用记录结果.在收缩左界的时候进行记录(判断). code: import heapq class Solution: def __init__(self): self.a=None sel

908. Smallest Range I - LeetCode

Description: Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i]. After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minim

Leetcode-908 Smallest Range I(最小差值 I)

1 class Solution 2 { 3 public: 4 int smallestRangeI(vector<int>& A, int K) 5 { 6 int Max = INT_MIN; 7 int Min = INT_MAX; 8 for(auto d:A) 9 { 10 if(d > Max) 11 Max = d; 12 if(d < Min) 13 Min = d; 14 } 15 16 if(Min+2*K>=Max) 17 return 0;

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【LeetCode】双指针 two_pointers(共47题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [19]Remove Nth Node From End of List [26]Remove Duplicates from Sorted