Skip to content

Commit 3acef1a

Browse files
committed
Implement task-scheduler
name: task-scheduler url: https://leetcode.com/problems/task-scheduler difficulty: medium time: 132ms time-rank: 48.86% time-complexity: O(N) space: 34.9MB space-rank: 66.22% space-complexity: O(1) Fixes #271 Signed-off-by: Christopher Friedt <[email protected]>
1 parent bb87402 commit 3acef1a

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

task-scheduler-test.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2021, Christopher Friedt
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#include <gtest/gtest.h>
8+
9+
#include "task-scheduler.cpp"
10+
11+
using namespace std;
12+
13+
TEST(TaskScheduler, AAABBB_2) {
14+
vector<char> tasks = {'A', 'A', 'A', 'B', 'B', 'B'};
15+
int n = 2;
16+
EXPECT_EQ(8, Solution().leastInterval(tasks, n));
17+
}
18+
19+
TEST(TaskScheduler, AAABBB_0) {
20+
vector<char> tasks = {'A', 'A', 'A', 'B', 'B', 'B'};
21+
int n = 0;
22+
EXPECT_EQ(6, Solution().leastInterval(tasks, n));
23+
}
24+
25+
TEST(TaskScheduler, AAAAAABCDEFG_2) {
26+
vector<char> tasks = {'A', 'A', 'A', 'A', 'A', 'A',
27+
'B', 'C', 'D', 'E', 'F', 'G'};
28+
int n = 2;
29+
EXPECT_EQ(16, Solution().leastInterval(tasks, n));
30+
}
31+
32+
TEST(TaskScheduler, AAABBB_50) {
33+
vector<char> tasks = {'A', 'A', 'A', 'B', 'B', 'B'};
34+
int n = 50;
35+
EXPECT_EQ(104, Solution().leastInterval(tasks, n));
36+
}
37+
38+
TEST(TaskScheduler, AAABBBCCCDDE_2) {
39+
vector<char> tasks = {'A', 'A', 'A', 'B', 'B', 'B',
40+
'C', 'C', 'C', 'D', 'D', 'E'};
41+
int n = 2;
42+
EXPECT_EQ(12, Solution().leastInterval(tasks, n));
43+
}
44+
45+
TEST(TaskScheduler, AABCDEFGHIJKLMNOPQRSTUVWXYZ_2) {
46+
vector<char> tasks = {'A', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
47+
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
48+
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
49+
int n = 31;
50+
EXPECT_EQ(33, Solution().leastInterval(tasks, n));
51+
}
52+
53+
TEST(TaskScheduler, ABCDEABCDE_4) {
54+
vector<char> tasks = {'A', 'B', 'C', 'D', 'E', 'A', 'B', 'C', 'D', 'E'};
55+
int n = 4;
56+
EXPECT_EQ(10, Solution().leastInterval(tasks, n));
57+
}
58+
59+
TEST(TaskScheduler, ABCDEFGHIJKLMNOPQRSTUVWXYZ_15) {
60+
vector<char> tasks = {
61+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
62+
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
63+
'U', 'V', 'W', 'X', 'Y', 'Z'
64+
};
65+
int n = 15;
66+
EXPECT_EQ(26, Solution().leastInterval(tasks, n));
67+
}

task-scheduler.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2021, Christopher Friedt
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#include <algorithm>
8+
#include <unordered_map>
9+
#include <vector>
10+
11+
using namespace std;
12+
13+
// name: task-scheduler
14+
// url: https://leetcode.com/problems/task-scheduler
15+
// difficulty: 2
16+
17+
class Solution {
18+
public:
19+
int leastInterval(vector<char> &tasks, int n) {
20+
if (0 == n) {
21+
return tasks.size();
22+
}
23+
24+
unordered_map<char, int> hist;
25+
int peak = 0;
26+
// create a histogram of task frequencies, also keep track of peak frequency
27+
for (const auto &task : tasks) {
28+
hist[task]++;
29+
peak = max(peak, hist[task]);
30+
}
31+
32+
if (hist.size() == tasks.size()) {
33+
return tasks.size();
34+
}
35+
36+
// find the number of tasks that share the peak frequency
37+
int npeak = 0;
38+
for (auto &kv : hist) {
39+
if (kv.second == peak) {
40+
++npeak;
41+
}
42+
}
43+
44+
return max(int(tasks.size()), (peak - 1) * (n + 1) + npeak);
45+
}
46+
};

0 commit comments

Comments
 (0)