Skip to content

Commit 59102cd

Browse files
committed
feat: add solution for task scheduler in rust
1 parent 295c8bb commit 59102cd

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/medium/task_scheduler.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#![allow(dead_code)]
2+
use std::collections::{BinaryHeap, VecDeque};
3+
4+
fn least_interval(tasks: Vec<char>, n: usize) -> usize {
5+
let mut heap = BinaryHeap::new();
6+
let mut q = VecDeque::new();
7+
let mut map = vec![0; 26];
8+
9+
for t in tasks {
10+
map[(t as u8 - 'A' as u8) as usize] += 1;
11+
}
12+
13+
for (i, cnt) in map.into_iter().enumerate() {
14+
if cnt > 0 {
15+
let c = ('A' as u8 + i as u8) as char;
16+
heap.push((cnt, c));
17+
}
18+
}
19+
20+
let mut time = 0;
21+
22+
while !heap.is_empty() || !q.is_empty() {
23+
while let Some(item) = q.pop_front() {
24+
let (process_time, cnt, c) = item;
25+
26+
if process_time < time {
27+
heap.push((cnt, c));
28+
} else {
29+
q.push_front(item);
30+
break;
31+
}
32+
}
33+
34+
if let Some((cnt, c)) = heap.pop() {
35+
if cnt > 1 {
36+
q.push_back((time + n, cnt - 1, c));
37+
}
38+
}
39+
40+
time += 1;
41+
}
42+
43+
time
44+
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use super::*;
49+
50+
#[test]
51+
fn test_1() {
52+
let tasks = vec!['A', 'A', 'A', 'B', 'B', 'B'];
53+
let n = 2;
54+
assert_eq!(least_interval(tasks, n), 8);
55+
}
56+
57+
#[test]
58+
fn test_2() {
59+
let tasks = vec!['A', 'A', 'A', 'B', 'B', 'B'];
60+
let n = 0;
61+
assert_eq!(least_interval(tasks, n), 6);
62+
}
63+
64+
#[test]
65+
fn test_3() {
66+
let tasks = vec!['A', 'A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'E', 'F', 'G'];
67+
let n = 2;
68+
assert_eq!(least_interval(tasks, n), 16);
69+
}
70+
}

src/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
- [x] [347. Top k frequent elements](../src/medium/top_k_frequent_elements.rs) -> [Problem Description](../src/medium/readme.md#347-top-k-frequent-elements)
8686
- [x] [424. Longest repeating character replacement](../src/medium/longest_repeating_character_replacement.rs) -> [Problem Description](../src/medium/readme.md#424-longest-repeating-character-replacement)
8787
- [x] [567. Permutation in string](../src/medium/permutation_in_string.rs) -> [Problem Description](../src/medium/readme.md#567-permutation-in-string)
88+
- [x] [621. Task scheduler](../src/medium/task_scheduler.rs) -> [Problem Description](../src/medium/readme.md#621-task-scheduler)
8889
- [x] [739. Daily temperatures](../src/medium/daily_temperatures.rs) -> [Problem Description](../src/medium/readme.md#739-daily-temperatures)
8990
- [x] [853. Car fleet](../src/medium/car_fleet.rs) -> [Problem Description](../src/medium/readme.md#853-car-fleet)
9091
- [x] [875. Koko eating bananas](../src/medium/koko_eating_bananas.rs) -> [Problem Description](../src/medium/readme.md#875-koko-eating-bananas)

theory/categories/6.heap_priority_queue/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ A priority queue can be implemented using a heap.
6767
- [x] [Last Stone Weight](https://leetcode.com/problems/last-stone-weight/) | Easy | [Solution](../../../src/easy/last_stone_weight.rs) | [Problem Description](../../../src/easy/readme.md#1046-last-stone-weight)
6868
- [x] [K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | Medium | [Solution](../../../src/medium/k_closest_points_to_origin.rs) | [Problem Description](../../../src/medium/readme.md#973-k-closest-points-to-origin)
6969
- [x] [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | Medium | [Solution](../../../src/medium/kth_largest_element_in_an_array.rs) | [Problem Description](../../../src/medium/readme.md#215-kth-largest-element-in-an-array)
70-
- [ ] [Task Scheduler](https://leetcode.com/problems/task-scheduler/) | Medium | [Solution](../../../src/medium/task_scheduler.rs) | [Problem Description](../../../src/medium/readme.md#621-task-scheduler)
70+
- [x] [Task Scheduler](https://leetcode.com/problems/task-scheduler/) | Medium | [Solution](../../../src/medium/task_scheduler.rs) | [Problem Description](../../../src/medium/readme.md#621-task-scheduler)
7171
- [ ] [Design Twitter](https://leetcode.com/problems/design-twitter/) | Medium | [Solution](../../../src/medium/design_twitter.rs) | [Problem Description](../../../src/medium/readme.md#355-design-twitter)
7272
- [ ] [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | Hard | [Solution](../../../src/hard/find_median_from_data_stream.rs) | [Problem Description](../../../src/hard/readme.md#295-find-median-from-data-stream)
7373

7474
Category: `Heap / Priority Queue`
7575
Created on: 2023-11-02 21:12
76-
Last modified on: 2023-11-03 19:02
76+
Last modified on: 2023-11-03 22:13
7777
Status: In Progress
7878
Author: [David Bujosa](https://github.com/bujosa)

0 commit comments

Comments
 (0)