2024每日刷题(136)
Leetcode—2244. 完成所有任务需要的最少轮数
实现代码
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 roundsfor(auto [_, freq]: map) {if(freq == 1) {return -1;} else {ans += (freq + 2) / 3;}}return ans;}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!