leetcode 621.Task Scheduler

爬虫-Playwright逆向小红书 【代码】爬虫-Playwright逆向小红书。 阅读详情

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.

You need to return the least number of intervals the CPU will take to finish all the given tasks.

Example:

Input: tasks = [“A”,“A”,“A”,“B”,“B”,“B”], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.

给出一个tasks的数组,代表不同的task,一个周期CPU可以选择处理一个task,也可以闲置
而且处理相同的task的时候,中间必须有n次闲置

比如上面例子中一个A到下一个A之间必须有n=2次闲置

思路:
为了不闲置,尽量每次处理不同的task
比如上面的例子,把不同的task先分组放在一起,一次处理一个由不同task组成的组,在这个组内就不需要闲置
而且每组的顺序应该是一样,这样才能保证相同的task间隔最远

AB AB AB

这样相同的A之间隔了一个B,但是题中要求n=2
也就是两个A之间需要间隔两个周期
因为少了一个,所以补一个idle(闲置),可以看到最后的一组后面是不需要补的
AB(idle)AB(idle)AB

而能留到最后一组的就是frequency最高的几个task
比如[A,A,A,A,B,B,C]
分组后(假设n=2)
ABC AB(idle) A(idle)(idle) A

可以看到需要的时间周期是由最大frequency的task决定的,由于最后一个不需要加idle
所以不看最后一个的时候,需要的时间周期是
(maxFrequency - 1) * (n + 1) //(n+1)是因为还要加上自身task

再加上最后一组,也就是最大frequency的task个数
(maxFrequency - 1) * (n - 1) + maxFrequencyNum

不过还有一种特殊情况,比如分组后task是ABC ABC,而n=1的情况
可以看到相同task间的间隔是>n的,但是如果按上述方法n=1进行计算
结果result就会比task的总个数小,这时候需要取
max(tasksNum, result)

//2ms
    public int leastInterval(char[] tasks, int n) {
        if (tasks == null || tasks.length == 0) {
            return 0;
        }
        
        if (n == 0) {
            return tasks.length;
        }
        
        int[] count = new int[26];
        int maxCount = 0;
        int res = 0;
        int sameFreq = 0;       
        
        for (int i = 0; i < tasks.length; i++) {
            count[tasks[i] - 'A'] ++;
        }
        
        for (int i = 0; i < count.length; i++) {
            if (count[i] > maxCount) {
            	maxCount = count[i];
            };
        }
        
        for (int i = 0; i < count.length; i++) {
            if (count[i] == maxCount) {
            	sameFreq ++;
            };
        }
        
        res = (maxCount - 1) * (n + 1) + sameFreq;
        
        return Math.max(tasks.length, res);
        
    }

用c++就会方便很多
count_if中用到lambda表达式,需要用到前面的max_count,所以放在[max_count]
lambda表达式中也需要加;
另外max比较的时候不能是int类型,所以res的类型是size_t

//48ms
class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) {
        vector<int> count(26, 0);
        
        for (int i = 0; i < tasks.size(); i++) {
            count[tasks[i] - 'A'] ++;
        }
        
        int max_count = *max_element(count.cbegin(), count.cend());
        int same_freq = count_if(count.cbegin(), count.cend(), 
                                 [max_count](int c){return c == max_count;});
        size_t res = (max_count - 1) * (n + 1) + same_freq;
        
        return max(tasks.size(), res);
    }
    
};
【LAMMPS学习】五、LAMMPS命令(8) Pair_style 势 所有LAMMPS pair_style命令如下表所示。有些样式有加速版本。这由括号中的附加字母表示:g = GPU, i = INTEL, k = KOKKOS, o = OPENMP, t = OPT。 阅读详情

相关推荐

半桥LLC谐振变换器及同步整流MATLAB仿真(二)

接上文,半桥LLC谐振变换器及同步整流MATLAB仿真

qq_55203246的博客 4350

[leetcode] 621 Task Scheduler

【题目描述】 Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each ta

yj12588的专栏 470

人工智能在高中教育中的应用现状剖析与挑战应对

人工智能在高中教育中的应用现状剖析与挑战应对

燕鹏 1347

leetcode621. Task Scheduler

题目Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could

xcy14的博客 444

用户消费行为分析(MapReduce实现,Hive分析)

一、题目要求 数据来自CDNow网站的一份用户购买CD明细,本数据集共有 6 万条 左右数据,数据为 CDNow 网站 1997年1月至1998年6月的用户行为 数据

紫烟的博客 3025

视频帧插学习(三):ema-vfi代码拆解和分析

video interp EMA-VFI

mikhailbran的博客 722

leetcode 621 Task Scheduler

leetcode 621

朝着梦想前行。 4439

癌症免疫治疗预测神器TIDE:从原理到实战(避坑指南)

本文详细解析了癌症免疫治疗预测工具TIDE的核心原理及实战应用。TIDE算法通过模拟肿瘤免疫逃逸的两种核心机制(T细胞功能障碍和排除),为临床决策提供精准预测。文章涵盖数据准备、标准化流程、网页工具操作指南及结果解读,帮助科研人员和临床医生避开常见误区,优化免疫治疗策略。

weixin_29309475的博客 208

[LeetCode]621. Task Scheduler

题意:模拟CPU的任务调度。给定一组字符数组和数值n,字符数组中每个字符表示一种任务,每个任务需要一个单位时间完成,n表示相同任务之间必须有n个单位时间间隔着。求完成所有任务需要的时间。

哈哈哈哈嘿嘿嘿 641

RM机器人麦轮底盘实战:从零搭建到PID调参全记录(附避坑指南)

本文详细记录了RM机器人麦轮底盘从零搭建到PID调参的全过程,涵盖硬件组装、开发环境搭建、底盘解算和PID调参等关键步骤,并附有实用的避坑指南。特别针对麦轮控制、底盘解算和电机调试等核心问题提供了专业解决方案,帮助开发者快速提升机器人性能。

bush的博客 400

Android13无重力感应系统修改触摸屏跟随屏幕显示旋转方向旋转

在实际产品中设计时会移除重力传感器。当设备缺少重力传感器时,系统无法使用该传感器提供的方向数据来确定屏幕旋转的方向。Android 系统中由于移除重力传感器导致的默认显示方向旋转引起触摸屏与屏幕显示不一致问题,主要是由于缺乏方向检测和触摸坐标的正确转换。通过使用替代传感器(如陀螺仪)、手动配置方向、调整触摸事件坐标和 UI 布局,可以解决这个问题。开发者需要确保在设备没有重力传感器的情况下,能够通过其他方法推测方向,并确保触摸事件和 UI 动作能够正确响应设备的旋转。

weixin_35723192的博客 812

LeetCode 621. Task Scheduler

621. Task Scheduler一、问题描述 Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without origin

李宁宁 3902

视觉Transformer实战 | Cross-Attention Multi-Scale Vision Transformer(CrossViT)详解与实现

Vision Transformer (ViT) 在计算机视觉领域取得了巨大成功,但标准 ViT 结构在处理不同尺度的视觉特征时存在局限性。双分支 ViT (Cross-Attention Multi-Scale Vision Transformer, CrossViT) 通过引入双分支结构来解决这一问题,能够同时捕获局部和全局视觉特征。本文将详细介绍 CrossViT 的技术原理,并使用 PyTorch 从零开始实现 CrossViT 模型。

盼小辉丶的博客 733

LeetCode - 621. Task Scheduler(贪心)

LeetCode - 621. Task Scheduler(贪心) 题目链接 题目 解析 // class Solution { // public int leastInterval(char[] tasks, int n) { // HashMap&amp;amp;lt;Character, Integer&amp;amp;gt; counts = new HashMap&amp;amp;lt;&amp;amp;gt;();...

zxzxin的博客~ 896

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

【leetcode-Python】-贪心-621. Task Scheduler

题目链接 https://leetcode.com/problems/task-scheduler/ 题目描述 给定字符数组tasks,表示CPU需要执行的任务列表。不同的字母表示不同种类的任务。任务可以以任意顺序执行,并且每个任务都可以在1个单位时间内执行完。 在任何一个单位时间,CPU可以完成一个任务,也可以处于待命状态。但是两个相同种类的任务之间必须有长度为整数n的冷却时间,因此至少有连续n个单位时间内CPU在执行不同的任务,或者在待命状态。计算完成所有任务所需的最短时间。 示例 输入:

gulaixiangjuejue的博客 375

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Leetcode 621. Task Scheduler (cpp)

题目 解法 这题关键在于计算所需要添加的idle time,而这个idle time由出现最多次的task决定。不过证明就有点麻烦了,可以看这边:https://leetcode.com/problems/task-scheduler/discuss/824421/A-math-proof class Solution { public: int leastInterval(vector<char>& tasks, int n) { vector<int&

qq_37821701的博客 356

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode算法学习日志-621 Task Scheduler

Leetcode 621 Task Scheduler 题目原文 Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done with

Zarlove的博客 1867

【LeetCode】621. Task Scheduler

621. Task Scheduler Description: Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without...

Freemanzxp 298

Leetcode 621 Task Scheduler

Leetcode 621 Task Scheduler Approach #1 Using Sorting [Accepted] But, there is a flaw in the above idea. Consider the case, where say the number of instances of tasks A, B, C, D, E are 6, 1, 1, 1, 1 r...

weixin_43876597的博客 133

LeetCode //C - 621. Task Scheduler

【代码】LeetCode //C - 621. Task Scheduler。

Made in Code 2093

[Leetcode] 621 Task Scheduler

题目要求 Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task co...

qingjian2052的博客 309

leetcode 621. Task Scheduler

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could b

淡然坊 671

[leetcode] 621. Task Scheduler

Description Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each...

Learning from the mistakes 302

[leetcode] 621. Task Scheduler @ python

原题 Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task cou...

闲庭信步 505

[leetcode]621. Task Scheduler

[leetcode]621. Task Scheduler Analysis 中午吃啥—— [每天刷题并不难0.0] Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different task...

weixin_32135877的博客 185

Leetcode-Medium 621. Task Scheduler

题目描述 给定一个char数组表示CPU需要执行的任务。它包含大写字母A到Z,其中不同的字母代表不同的任务。每项任务都可以在一个时间间隔内完成。对于每个间隔,CPU可以完成一个任务或空闲。但是有规定一个非负整数n代表两个相同任务之间需要至少n个时间单位。球最少数量的时间单位完成所有任务。 需要返回CPU将完成所有给定任务所需的最少间隔数。 实例: Inpu...

yanqianglifei的专栏 304
上一篇: leetcode 435. Non-overlapping Intervals
下一篇: leetcode 167. Two Sum II - Input array is sorted
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址