Repository navigation
Expand file tree
/
Copy pathTaskScheduling.cs
More file actions
70 lines (61 loc) · 2.59 KB
/
Copy pathTaskScheduling.cs
File metadata and controls
70 lines (61 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
namespace HackerRank.Solutions.DataStructures;
/// <summary>
/// Task Scheduling — tasks arrive one by one, each with a deadline and a duration in minutes.
/// After each arrival, report the smallest possible value of the largest overshoot (finish time
/// minus deadline, never below 0) when the tasks that have arrived so far are run back to back
/// in the best order.
/// The best order is by deadline, so a task finishes at the total duration of every task whose
/// deadline is at most its own. A segment tree indexed by deadline holds, for each deadline d,
/// "total duration of tasks due by d minus d": adding a task of duration m with deadline d adds m
/// to every position from d onwards, and the answer is the tree-wide maximum. O(n log n).
/// https://www.hackerrank.com/challenges/task-scheduling
/// </summary>
public static class TaskScheduling
{
/// <param name="tasks">Each task as its deadline and its duration in minutes, in arrival order.</param>
/// <returns>The minimal maximum overshoot after each task has arrived.</returns>
public static int[] Solve(IReadOnlyList<(int Deadline, int Minutes)> tasks)
{
int size = tasks.Count == 0 ? 1 : tasks.Max(t => t.Deadline);
// best[node] is the maximum of its range including the node's own pending addition.
var best = new long[4 * size];
var added = new long[4 * size];
Build(best, 1, 1, size);
var result = new int[tasks.Count];
for (int i = 0; i < tasks.Count; i++)
{
Add(best, added, 1, 1, size, tasks[i].Deadline, size, tasks[i].Minutes);
result[i] = (int)Math.Max(0, best[1]);
}
return result;
}
private static void Build(long[] best, int node, int lo, int hi)
{
if (lo == hi)
{
best[node] = -lo;
return;
}
int mid = (lo + hi) / 2;
Build(best, 2 * node, lo, mid);
Build(best, 2 * node + 1, mid + 1, hi);
best[node] = Math.Max(best[2 * node], best[2 * node + 1]);
}
private static void Add(long[] best, long[] added, int node, int lo, int hi, int from, int to, int value)
{
if (from > hi || to < lo)
{
return;
}
if (from <= lo && hi <= to)
{
best[node] += value;
added[node] += value;
return;
}
int mid = (lo + hi) / 2;
Add(best, added, 2 * node, lo, mid, from, to, value);
Add(best, added, 2 * node + 1, mid + 1, hi, from, to, value);
best[node] = Math.Max(best[2 * node], best[2 * node + 1]) + added[node];
}
}