编程语言
首页 > 编程语言> > Leetcode 1509. Minimum Difference Between Largest and Smallest Value in Three Moves [Python]

Leetcode 1509. Minimum Difference Between Largest and Smallest Value in Three Moves [Python]

作者:互联网

可处理三次,那么能处理的其实是“丢掉”3个最小的数字或者3个最大的数字,或者6个数字里选3个(最小,次小,最大)(最小,次大,最大),所以也就是要在这4种subarray中选出最大最小值差最小的那个。
先sort,选出上述4种newnums。计算差值,找最小。

class Solution:
    def minDifference(self, nums: List[int]) -> int:
        if len(nums) <= 4:return 0
        nums.sort()
        newnum1 = nums[:-3]
        newnum2 = nums[1:-2]
        newnum3 = nums[2:-1]
        newnum4 = nums[3:]
        arr = [ newnum1, newnum2, newnum3, newnum4]
        res = float('inf')
        for thenums in arr:
            res = min(res, max(thenums) - min(thenums))
        return res

标签:选出,数字,nums,Python,最小,int,Between,Difference,最大
来源: https://blog.csdn.net/sinat_30403031/article/details/122791733