Leetcode—2244. 完成所有任务需要的最少轮数【中等】

2024每日刷题(136)

Leetcode---2244. 完成所有任务需要的最少轮数

实现代码

cpp 复制代码
class Solution {
public:
    int minimumRounds(vector<int>& tasks) {
        unordered_map<int, int> map;
        for(int task: tasks) {
            map[task]++;
        }
        int ans = 0;

        // freq = 1 -> it's impossible
        // freq = 2 -> needs 1 round
        // freq = 3 -> needs 1 round
        // freq = 3k                           -> needs k rounds
        // freq = 3k + 1 = 3 * (k - 1) + 2 * 2 -> needs k + 1 rounds
        // freq = 3k + 2 = 3 * k       + 2 * 1 -> needs k + 1 rounds
        for(auto [_, freq]: map) {
            if(freq == 1) {
                return -1;
            } else {
                ans += (freq + 2) / 3;
            }
        }
        return ans;
    }
};

运行结果


之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
legendary_bruce35 分钟前
【22-决策树】
算法·决策树·机器学习
浪成电火花1 小时前
(deepseek!)deepspeed中C++关联部分
开发语言·c++
独行soc1 小时前
2025年渗透测试面试题总结-18(题目+回答)
android·python·科技·面试·职场和发展·渗透测试
max5006002 小时前
基于桥梁三维模型的无人机检测路径规划系统设计与实现
前端·javascript·python·算法·无人机·easyui
愿天堂没有C++3 小时前
剑指offer第2版——面试题4:二维数组中的查找
c++·面试
快去睡觉~4 小时前
力扣400:第N位数字
数据结构·算法·leetcode
徐归阳4 小时前
第二十四天:虚函数与纯虚函数
c++
青草地溪水旁4 小时前
UML函数原型中constraint的含义,有啥用?
c++·uml
qqxhb5 小时前
零基础数据结构与算法——第七章:算法实践与工程应用-搜索引擎
算法·搜索引擎·tf-idf·倒排索引·pagerank·算法库
gzzeason6 小时前
LeetCode Hot100:递归穿透值传递问题
算法·leetcode·职场和发展