Minimum Difference Between Largest and Smallest Value in Three Moves LeetCode solution in Java

1
class Solution {
2
public int minDifference(int[] nums) {
3
// sort the nums
4
// to gain the mini difference
5
// we want to remove the three smallest or biggest
6
// 0 - 3
7
// 1 - 2
8
// 2 - 1
9
// 3 - 0
10
if (nums.length <= 4) {
11
return 0;
12
}
13

14
Arrays.sort(nums);
15

16
int left = 0, right = 3;
17

18
int res = Integer.MAX_VALUE;
19
while (left <= 3) {
20
int mini = nums[left];
21
int max = nums[nums.length - right - 1];
22
res = Math.min(res, max - mini);
23

24
left++;
25
right--;
26
}
27

28
return res;
29
}
30
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0