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

第269场周赛钛铂数据T3.从数组中移除最大值和最小值

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

第269场周赛钛铂数据T3.从数组中移除最大值和最小值

解题思路

贪心,每次都采用最操作次数最小的删除策略
分情况讨论

代码
class Solution {
    public int minimumDeletions(int[] nums) {
        if (nums.length <= 1){
            return nums.length;
        }
        int max = Integer.MIN_VALUE,min = Integer.MAX_VALUE;
        int maxIndex = -1,minIndex = -1;
        for (int i=0;i max){
                max = nums[i];
                maxIndex = i;
            }
        }
        int ope = 0;
        //先删掉其中离两端较近的一个,然后找到删除操作后的下标位置
        int maxDis = Math.min(maxIndex+1,nums.length-maxIndex);
        int minDis = Math.min(minIndex+1,nums.length-minIndex);
        int index = -1;
        ope += Math.min(maxDis,minDis);
        //先删除最大值
        if (minDis > maxDis){
            if (maxDis == maxIndex+1){
                index = maxIndex+1;
            }else {
                index = nums.length-maxDis;
            }
            if (index > minIndex){
                ope += Math.min(index-minIndex,minDis);
            }else {
                ope += Math.min(minIndex+1-index,minDis);
            }
        }else {
            if (minDis == minIndex+1){
                index = minIndex+1;
            }else {
                index = nums.length-minDis;
            }
            if (index > maxIndex){
                ope += Math.min(index-maxIndex,maxDis);
            }else {
                ope += Math.min(maxIndex+1-index,maxDis);
            }
        }
        return ope;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/648779.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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