栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/738592.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号