【无标题】

基础版:

class Solution {
public:
    int minimumRounds(vector<int>& tasks) {
        // 先排序
        sort(begin(tasks), end(tasks));

        int ans = 0;

        // 依据count,计算完成该任务的最小轮数,这里count小于等于1的情况
        auto fun0 = [](int count)
        {
            // 计算前一类任务所需的最小轮数
            int remainder = count % 3;
            if(remainder == 0){
                return (count / 3);
            }else if(remainder == 1){
                // 先执行2轮难度级别为nowPre的任务,每轮2个任务
                count -= 4;
                int ans = 2;

                // 剩余的全部为每轮都执行3次
                return ans + (count / 3);
            }else
            {
            //}else if(remainder == 2){
                // 先执行1轮难度级别为nowPre的任务,每轮2个任务
                count -= 2;
                int ans = 1;

                // 剩余的全部为每轮都执行3次
                return ans + (count / 3);
            }           
        };

        int nowPre = tasks[0], count = 0;
        for(int i = 0; i < tasks.size(); i++){
            if(nowPre == tasks[i]){
                count++;
            }else{
                // 这类任务个数为1时,直接返回结果
                if(count == 1){
                    return -1;
                }

                // 计算前一类任务
                ans += fun0(count);

                // 记录当前任务
                nowPre = tasks[i];
                count = 1;
            }
        }

        // 这类任务个数为1时,直接返回结果
        if(count == 1){
            return -1;
        }

        // 计算最后一类任务
        ans += fun0(count);

        return ans; 
    }
};

 

参考优化版:

当余数为1和2时,其实最后的最小处理次数是一样的。

class Solution {
public:
    int minimumRounds(vector<int>& tasks) {
        // 先排序
        sort(begin(tasks), end(tasks));

        int ans = 0;

        // 依据count,计算完成该任务的最小轮数,这里count小于等于1的情况
        auto fun0 = [](int count)
        {
            // 计算前一类任务所需的最小轮数
            int remainder = count % 3;
            if(remainder == 0){
                return (count / 3);
            }else
            {
                // 优化点,分析可知,其实不管余数为1还是2,最小的执行轮数都为下面的结果
                return (count / 3) + 1;
            }

            // }else if(remainder == 1){
            //     // 先执行2轮难度级别为nowPre的任务,每轮2个任务
            //     count -= 4;
            //     int ans = 2;

            //     // 剩余的全部为每轮都执行3次
            //     return ans + (count / 3);
            // }else
            // {
            // //}else if(remainder == 2){
            //     // 先执行1轮难度级别为nowPre的任务,每轮2个任务
            //     count -= 2;
            //     int ans = 1;

            //     // 剩余的全部为每轮都执行3次
            //     return ans + (count / 3);
            // }           
        };

        int nowPre = tasks[0], count = 0;
        for(int i = 0; i < tasks.size(); i++){
            if(nowPre == tasks[i]){
                count++;
            }else{
                // 这类任务个数为1时,直接返回结果
                if(count == 1){
                    return -1;
                }

                // 计算前一类任务
                ans += fun0(count);

                // 记录当前任务
                nowPre = tasks[i];
                count = 1;
            }
        }

        // 这类任务个数为1时,直接返回结果
        if(count == 1){
            return -1;
        }

        // 计算最后一类任务
        ans += fun0(count);

        return ans; 
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值