---
# System prepended metadata

title: 1509. Minimum Difference Between Largest and Smallest Value in Three Moves
tags: [Leetcode, Bloomberg, Sliding Window, Google, Medium]

---

# 1509. Minimum Difference Between Largest and Smallest Value in Three Moves
###### tags: `Leetcode` `Medium` `Google` `Bloomberg` `Sliding Window`
Link: https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/
## 思路
这道题目其实就是在阵列的最大的3个数里面和最小的3个数里面选择到底留哪三个数字
### 思路一
排序，滑动窗口
### 思路二
贪心算法 只维护最大的四个数和最小的四个数（不是维护三个数的原因是 如果抛弃了最大（小）的三个数，需要用最小（大）的数减去最大（小）的第四个数算差值

## Code
### 思路一
```java=
class Solution {
    public int minDifference(int[] nums) {
        if(nums.length <= 4){
            return 0;
        }
        Arrays.sort(nums);
        int min = Integer.MAX_VALUE;
        for(int i = 0;i <= 3;i++){
            min = Math.min(min, nums[i+nums.length-3-1]-nums[i]);
        }
        return min;
    }
}
```