Problema
await Task.WhenAll(tasks) starts a group of tasks together and continues when the slowest one finishes. A program that awaits several groups one after another therefore takes the sum of the slowest task of each group.
Implement when_all_total_time(groups), where groups[i] lists the durations of the tasks in group i.
Input
groups: The groups, awaited in order. An empty group finishes immediately.
Output
Return the total time.
Vincoli
0 ≤ number of groups ≤ 10,000
0 ≤ tasks per group ≤ 1,000; 0 ≤ duration ≤ 10^6
Esempi
Input:
groups = [
[3, 1, 2],
[5],
[2, 2]
]
Output atteso:
expected = 10
Input:
std::vector<std::vector<long long>> groups = {
{3, 1, 2},
{5},
{2, 2}
};
Output atteso:
long long expected = 10;
Input:
long[][] groups = new long[][] {
new long[] {3, 1, 2},
new long[] {5},
new long[] {2, 2}
};
Output atteso:
long expected = 10;
Spiegazione
The groups take as long as their slowest task: 3 + 5 + 2 = 10.
Input:
groups = [
[],
[4, 7]
]
Output atteso:
expected = 7
Input:
std::vector<std::vector<long long>> groups = {
{},
{4, 7}
};
Output atteso:
long long expected = 7;
Input:
long[][] groups = new long[][] {
new long[] {},
new long[] {4, 7}
};
Output atteso:
long expected = 7;
Spiegazione
The empty group takes no time; the second group takes 7.
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
A group finishes when its slowest task finishes.
Suggerimento 2
Groups run one after another, so their times add up.
Suggerimento 3
An empty group adds no time.
Complessità attesa
O(total number of tasks) time, O(1) extra space
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.