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 - min, 我们可以把初始最大差值设为max - min, 确保最终结果不会比这个平凡值更坏。
对于最后一种情况(min + k, max - k), 需要继续分情况讨论。
方便起见,我们可以把所有元素都预先-k, 然后从最小元素开始,尝试依次把各元素+2*k.
我们可以证明,如果选择a[i] + 2 * k,那么之前任一元素a[j] (0 <= j < i)加上 2 * k,都不会使结果更坏。证明见此:
B = [A[0] + 2K, ...,A[i-1]+2K, A[i] + 0, A[i+1] +2K, ..., A[j]+2K, ..., A[j+1]+0, A[n-1]+0]
B‘ = [A[0] + 2K, ...,A[i-1]+2K, A[i] + 2K, A[i+1] +2K, ..., A[j]+2K, ..., A[j+1]+0, A[n-1]+0]
A[i]+2K is between A[i-1] + 2K and A[i+1] +2K, so it must stand in the range of B.
B‘ is not worse than B, it can be easily generalized to multiple elements added by 0 between the ones added by 2K.
于是当我们考虑a[i] + 2 * k时,可以假设之前的元素都已经加上了2 * k。
当前最大差值取决于:
1) 当前数列最小值:
可能为a[i+1]
a[i+1],...a[0]+2*k...a[n-1]...
或a[0]+2*k
a[0]+2*k..., a[i+1],...a[n-1]...
2) 以及当前数列最大值:
可能为a[i]+2*k
...a[n-1]...a[i]+2*k
或者a[n-1]
...a[i]+2*k...a[n-1]

因此我们只需遍历所有元素,计算当前元素加上2*k的以后的数组的最大差值,取其中的最小值即可。

public:
    int smallestRangeII(vector<int>& a, int k) {
        size_t len = a.size();
        if(len <= 1) return 0;
        sort(a.begin(), a.end());
        int front = a.front(), back = a.back(), d = back - front, start = front + 2 * k;
        if( k >= d) return d;
        int ans = d;
        for(size_t i = 0; i < len - 1; i++){
            int lo = min(start, a[i + 1]), hi = max(a[i] + 2 * k, back);
            ans = min(ans, hi - lo);
        }
        return ans;
    }

参考:https://zhanghuimeng.github.io/post/leetcode-910-smallest-range-ii/

原文地址:https://www.cnblogs.com/k330/p/LeetCode_910_Smallest_Range_II.html

时间: 2024-10-09 12:16:49

LeetCode 910. 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] 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:

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 (

(LeetCode)Pascal&#39;s Triangle II --- 杨辉三角进阶(滚动数组思想)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Subscribe to see which companies asked this question 解题分析: 此处有空间的限制,因此不能正

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

一道题目- 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——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one