当前位置:网站首页>2022-06-25: given a positive number n, it means that there are tasks 0~n-1. Given an array of length N, time[i] means the time when task I is completed. Given a two-dimensional array matrix, matrix[j]
2022-06-25: given a positive number n, it means that there are tasks 0~n-1. Given an array of length N, time[i] means the time when task I is completed. Given a two-dimensional array matrix, matrix[j]
2022-06-26 02:32:00 【Fuda scaffold constructor's daily question】
2022-06-25: Give a positive number n, Express 0~n-1 Task ,
Given a length of n Array of time,time[i] Express i When task No ,
Given a two-dimensional array matrix,
matrix[j] = {a, b} representative :a Task wants to start , rely on b The completion of the task ,
Any task that can be parallelized can be parallelized , But any task can only be completed by tasks that depend on it , To start .
Returns a length of n Array of ans, Indicates the completion time of each task .
Input guarantees no circular dependencies .
From meituan .3.26 written examination .
answer 2022-06-25:
Do dynamic planning based on topological sorting .
The code to use rust To write . The code is as follows :
fn main() {
let mut time:Vec<i32>=vec![5,3,4,2,7];
let mut matrix:Vec<Vec<i32>>=vec![vec![0,1],vec![0,2],vec![1,2],vec![3,1],vec![4,0]];
let ans = finish_time(5,&mut time,&mut matrix);
println!("ans = {:?}", ans);
}
fn finish_time(n: i32, time: &mut Vec<i32>, matrix: &mut Vec<Vec<i32>>) -> Vec<i32> {
let mut nexts: Vec<Vec<i32>> = vec![];
for i in 0..n {
nexts.push(vec![]);
}
let mut in0: Vec<i32> = vec![];
for _ in 0..n {
in0.push(0);
}
for line in matrix.iter() {
nexts[line[1] as usize].push(line[0]);
in0[line[0] as usize] += 1;
}
let mut zero_in_queue: Vec<i32> = vec![];
let mut ans: Vec<i32> = vec![];
for _ in 0..n {
ans.push(0);
}
for i in 0..n {
if in0[i as usize] == 0 {
zero_in_queue.push(i);
}
}
while zero_in_queue.len() > 0 {
let cur = zero_in_queue[0];
zero_in_queue.remove(0);
ans[cur as usize] += time[cur as usize];
for next in nexts[cur as usize].iter() {
ans[*next as usize] = get_max(ans[*next as usize], ans[cur as usize]);
in0[*next as usize] -= 1;
if in0[*next as usize] == 0 {
zero_in_queue.push(*next);
}
}
}
return ans;
}
fn get_max<T: Clone + Copy + std::cmp::PartialOrd>(a: T, b: T) -> T {
if a > b {
a
} else {
b
}
}
The results are as follows :
边栏推荐
- Agent challenge - "Olympic running"
- SDRAM controller -- implementation of arbitration module
- WPF window centering & change trigger mechanism
- Fastadmin applet assistant is purchased, but the work order cannot be published in the problem work order
- 股票开户怎么开户?网上开户是否安全么?
- 2022-06-25:给定一个正数n, 表示有0~n-1号任务, 给定一个长度为n的数组time,time[i]表示i号任务做完的时间, 给定一个二维数组matrix, matrix[j] = {a,
- In depth good article: what is supernetting?
- 购买了fastadmin小程序助手但是问题工单中无法发布工单
- Blazor University (33)表单 —— EditContext、FieldIdentifiers
- Mongoose - Why we make “mongoose.Promise = global.Promise” when setting a mongoose module?
猜你喜欢
Redis Lua沙盒绕过命令执行(CVE-2022-0543)
基于邻接矩阵的深度优先遍历实现
Ardiuno smart mosquito racket
树莓派 + AWS IoT Greengrass
Jenkins localization and its invalid solution
How to solve the problem that the iPhone 13 lock screen cannot receive the wechat notification prompt?
表达式的动态解析和计算,Flee用起来真香
Agent challenge - "Olympic running"
Depth first traversal based on adjacency table
Pointnet/pointnet++ learning
随机推荐
Advanced cross platform application development (23): an article approaching the launch of testlight
Fastadmin applet assistant is purchased, but the work order cannot be published in the problem work order
二分查找
Markov decision process (MDP): gambler problem
In depth good article: what is supernetting?
Redis6.0新特性——ACL(权限控制列表)实现限制用户可执行命令和KEY
图的广度优先遍历
基于邻接表的深度优先遍历
How to use commands to write file names (including paths) in folders to txt files
Ardiuno smart mosquito racket
win32
Gold three silver four~
. Net7 miniapi (special part):preview5 optimizes JWT verification (Part 2)
Meaning of each state in TCP network communication
Redis6.0 new feature - ACL (permission control list) implements the restriction of user executable commands and keys
Depth first traversal of Graphs
股票开户怎么开户?网上开户是否安全么?
微博评论的高性能高可用计算架构
Largeur d'abord traversée basée sur la matrice de contiguïté
OpenAPI 3.0 规范-食用指南