621. Task Scheduler Medium

@problem@discussion
#Array#Hash Table#Greedy#Sorting#Heap (Priority Queue)#Counting



1/**
2 * [621] Task Scheduler
3 *
4 * Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.
5 * However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array), that is that there must be at least n units of time between any two same tasks.
6 * Return the least number of units of times that the CPU will take to finish all the given tasks.
7 *  
8 * Example 1:
9 * 
10 * Input: tasks = ["A","A","A","B","B","B"], n = 2
11 * Output: 8
12 * Explanation: 
13 * A -> B -> idle -> A -> B -> idle -> A -> B
14 * There is at least 2 units of time between any two same tasks.
15 * 
16 * Example 2:
17 * 
18 * Input: tasks = ["A","A","A","B","B","B"], n = 0
19 * Output: 6
20 * Explanation: On this case any permutation of size 6 would work since n = 0.
21 * ["A","A","A","B","B","B"]
22 * ["A","B","A","B","A","B"]
23 * ["B","B","B","A","A","A"]
24 * ...
25 * And so on.
26 * 
27 * Example 3:
28 * 
29 * Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
30 * Output: 16
31 * Explanation: 
32 * One possible solution is
33 * A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	1 <= task.length <= 10^4
39 * 	tasks[i] is upper-case English letter.
40 * 	The integer n is in the range [0, 100].
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/task-scheduler/
46// discuss: https://leetcode.com/problems/task-scheduler/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn least_interval(tasks: Vec<char>, n: i32) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_621() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.