Todos los desafíos
Fácil 0 resueltos 0% de aceptación

WhenAll Total Time

Find how long a program takes when each group of tasks runs together with Task.WhenAll and groups run one after another.

Lo que aprenderás

  • Understand that WhenAll waits for the slowest task
  • Add up the time of groups awaited in order
  • Handle empty groups

Problema

Ver traducción

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.

Restricciones

0 ≤ number of groups ≤ 10,000

0 ≤ tasks per group ≤ 1,000; 0 ≤ duration ≤ 10^6

Ejemplos

Ejemplo 1
Entrada:
groups = [
    [3, 1, 2],
    [5],
    [2, 2]
]

Salida esperada:
expected = 10
Entrada:
std::vector<std::vector<long long>> groups = {
    {3, 1, 2},
    {5},
    {2, 2}
};

Salida esperada:
long long expected = 10;
Entrada:
long[][] groups = new long[][] {
    new long[] {3, 1, 2},
    new long[] {5},
    new long[] {2, 2}
};

Salida esperada:
long expected = 10;

Explicación

The groups take as long as their slowest task: 3 + 5 + 2 = 10.

Ejemplo 2
Entrada:
groups = [
    [],
    [4, 7]
]

Salida esperada:
expected = 7
Entrada:
std::vector<std::vector<long long>> groups = {
    {},
    {4, 7}
};

Salida esperada:
long long expected = 7;
Entrada:
long[][] groups = new long[][] {
    new long[] {},
    new long[] {4, 7}
};

Salida esperada:
long expected = 7;

Explicación

The empty group takes no time; the second group takes 7.

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

A group finishes when its slowest task finishes.

Pista 2

Groups run one after another, so their times add up.

Pista 3

An empty group adds no time.

Complejidad esperada

O(total number of tasks) time, O(1) extra space

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.