# Leetcode 910. Smallest Range II

Source: Internet
Author: User

A very interesting problem of mathematical reasoning, pruning after the solution is also very concise.
At first glance, it seems necessary to compare each number with other numbers. But the sorting can be found to be greatly simplified:
For any pair of elements a[i] < a[j], A[i]-K and A[j] + K can be ruled out, because there will be a greater difference than the original value, so for the original array of Min min Max, (min-k, Max + k) case can be excluded. The remaining three cases, (Min-k, Max-k), (min + k, max + K) and (min + K, max-k), and the latter two are equivalent to the original value of Max-min, we can set the initial maximum difference to max-min, to ensure that the final result is no more than this A common value is bad.
For the last case (min + K, max-k), it is necessary to continue the discussion of the situation.
For convenience, we can pre-K all the elements, then start with the smallest elements and try to +2*k the elements in turn.
We can prove that if you choose A[i] + 2 * k, then any previous element A[j] (0 <= J < i) plus 2 * k will not make the result worse. Proof See this:
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.
So when we consider A[i] + 2 * k, we can assume that the previous element has been added 2 * k.
The current maximum difference depends on:
1) The minimum value of the current sequence:
May be a[i+1]
A[I+1],... a[0]+2*k...a[n-1] ...
or A[0]+2*k
A[0]+2*k ..., a[i+1],... a[n-1] ...
2) and the maximum value of the current sequence:
Possible for A[i]+2*k
... a[n-1]...a[i]+2*k
or a[n-1]
... a[i]+2*k...a[n-1]

So we just go through all the elements, calculate the maximum difference between the current element plus the 2*k's future array, and take the minimum value.

` Public:    intSmallestrangeii (vector<int>& A,intk) {size_t len=a.size (); if(Len <=1)return 0;        Sort (A.begin (), A.end ()); intFront = A.front (), back = A.back (), d = back-front, start = front +2*K; if(k >= D)returnD; intAns =D;  for(size_t i =0; I < Len-1; i++){            intLo = min (start, A[i +1]), hi = Max (A[i] +2*K, back); Ans= min (ans, hi-lo); }        returnans; }`

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

Leetcode 910. Smallest Range II

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.