当前位置:网站首页>Buckle practice - 17 task scheduler
Buckle practice - 17 task scheduler
2022-07-18 10:56:00 【qq_ forty-three million four hundred and three thousand six hun】
17 Task scheduler
1. Problem description
Given an array of characters CPU List of tasks to be performed . It contains... In uppercase A - Z Letters indicate 26 There are different kinds of tasks . Tasks can be performed in any order , And every task can be in 1 In unit time .CPU You can perform a task in any unit of time , Or on standby .
However , There must be a length of n The cooling time of , So at least there's continuity n In units of time CPU On different missions , Or on standby .
You need to calculate the minimum time required to complete all tasks .
Example :
Input :tasks = [“A”,“A”,“A”,“B”,“B”,“B”], n = 2
Output :8
explain :A -> B -> ( On standby ) -> A -> B -> ( On standby ) -> A -> B.
In this example , The interval length between two tasks of the same type must be n = 2 The cooling time of , It takes only one unit time to perform a task , So in the middle ( On standby ) state .
explain :
The total number of tasks is [1, 10000].
n The value range of is [0, 100].
Refer to the following main function :
int main()
{
vector<char> v;
int len,n;
char data;
cin>>len;
for(int i=0; i<len; i++)
{
cin>>data;
v.push_back(data);
}
cin>>n;
int result=Solution().leastInterval(v,n);
cout<<result<<endl;
return 0;
}
2. Enter description
First enter the number of tasks len
Then input len A capital A - Z Letter , No spaces 、 No quotes
Last input n
3. The output shows that
Output an integer , Show the result
4. Example
Input
6
AAABBB
2
Output
8
5. Code
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
bool compare(int x, int y)
{
return x > y ;
}
int leastInterval(vector<char>tasks, int n)
{
// thought : Bucket thought , Set the size to n+1 My bucket , The task with the largest number of tasks
// Reference resources https://leetcode.cn/problems/task-scheduler/solution/tong-zi-by-popopop/
//1. Sort the number of occurrences of all tasks
//int len = tasks.size();// Get the total number of tasks
vector<int>vec(26);// Used to record the number of occurrences of each task
for (int i = 0; i < tasks.size(); i++)
{
vec[tasks[i] - 'A']++; // Skillfully count the number of times each character appears
}
sort(vec.begin(), vec.end(), compare);// Pay attention to this side compare Function writing , Use bool Return to comparison , Here is the descending order
//2. Count the tasks of the last bucket x
int cnt = 1;// Used to calculate the number of tasks of the last bucket x
while (cnt < vec.size()&& vec[cnt] == vec[0])// Be careful not to fall into a dead circle ,vec[cnt]==vec[0] Must be in while() in , Otherwise, there will be a dead cycle //while (cnt < vec.size())
{
//{ if(vec[cnt]==vec[0]) cnt++;}
//vec[0] Stored is the maximum number of tasks in all tasks N, After calculation vec[1],vec[2]etc Number of tasks and vec[0] Equal number , Calculation x
cnt++;
}
int x = cnt;
int N = vec[0];
//3. Compare NUM1 and NUM2 size ,NUM1=(N-1)*(n+1)+x; NUM2=tasks.size(); Return the largest
int NUM1 = (N - 1)*(n + 1) + x;
int NUM2 = tasks.size();
return max(NUM1, NUM2);
}
int main()
{
vector<char> v;
int len, n;
char data;
cin >> len;
for (int i = 0; i < len; i++)
{
cin >> data;
v.push_back(data);
}
cin >> n;
int result = leastInterval(v, n);
cout << result << endl;
return 0;
}
边栏推荐
- class path resource [xxx.properties] cannot be opened because it does not exist
- 广西二造备考仅剩1天 二级造价师考前三页纸来了 字字提分
- ACL访问控制列表案例(7.15)
- 2022数学建模“五一杯”B题
- Chrome 插件开发
- Thinking about the research links of data governance projects
- Execution plan of SQL statement
- 【MySQL必知必会】条件语句
- Redis - redis list function explanation and industrial application
- Issue record: “No thread for socket” about Memcached
猜你喜欢

The trick of scanning the mobile browser is to know everything and answer questions and translate

【MySQL必知必会】条件语句

Reinforcement Learning 强化学习(二)

十分钟快速学习flask框架

ASEMI整流桥GBJ2510规格,GBJ2510封装,GBJ2510特性

关于线程切换问题的一些思考总结

CentOS7.9安装MySQL8详细步骤

C 语言基础双指针移除元素解法

Who else can't use the three methods of de duplication in SQL?

Can SQL also do AI? you 're right! Mlops meetup V3 review openmlbd+sqlflow+byzer
随机推荐
Record the troubleshooting experience of a pit father memory leak
想学硬件,该学什么啊?
Viewing the self driving evolution of game phones from the Red Devils 7S series
class path resource [xxx.properties] cannot be opened because it does not exist
Talk about throwing eggs in the building again
【MySQL必知必会】条件语句
第17章 OAuth2LoginAuthenticationWebFilter 源码解析
ACL+ANT综合案例(7.15)
漫谈软件缺陷管理的价值
Basic SQL (I): install MySQL and some simple operations
stoi函数使用注意事项
(手工)【sqli-labs42、43】POST注入、堆叠注入、错误回显、字符型注入
Buckle practice - 23 lifeboats
ACL access control list case (7.15)
Paul Holland: predicting the trend of the financial market
Is it safe to open an account for flush stock
一文读懂软件测试的常见分类
【vulnhub】FIVE86: 1
SPFA variant question
C 语言基础双指针移除元素解法