Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
22 lines (18 loc) · 707 Bytes

File metadata and controls

22 lines (18 loc) · 707 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package LeetCode.array;
import java.util.Arrays;
public class LeetCode910 {
public int smallestRangeII(int[] A, int k) {
int n = A.length;
Arrays.sort(A); // 先排序
int res = A[n-1]-A[0];
// 首先 -k +k 可以转化为 0 2k
// 全部加 0/2k 结果是不变的,那么剩下的可能就是某一位置i加2k时的结果
// 最大值在A[i]+2k 与 A[ln-1] 中,最大的最小值在A[0]+2k 与 A[i+1] 中
for(int i = 0; i < n-1; i++) {
int max = Math.max(A[n-1], A[i]+2*k);
int min = Math.min(A[0]+2*k, A[i+1]);
res = Math.min(res, max-min);
}
return res;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.