簡単 0件解決済み 正答率0%

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.

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

0 ≤ number of groups ≤ 10,000

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

例

例 1
入力:
groups = [
    [3, 1, 2],
    [5],
    [2, 2]
]

期待される出力:
expected = 10
入力:
std::vector<std::vector<long long>> groups = {
    {3, 1, 2},
    {5},
    {2, 2}
};

期待される出力:
long long expected = 10;
入力:
long[][] groups = new long[][] {
    new long[] {3, 1, 2},
    new long[] {5},
    new long[] {2, 2}
};

期待される出力:
long expected = 10;

解説

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

例 2
入力:
groups = [
    [],
    [4, 7]
]

期待される出力:
expected = 7
入力:
std::vector<std::vector<long long>> groups = {
    {},
    {4, 7}
};

期待される出力:
long long expected = 7;
入力:
long[][] groups = new long[][] {
    new long[] {},
    new long[] {4, 7}
};

期待される出力:
long expected = 7;

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

A group finishes when its slowest task finishes.

ヒント 2

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

ヒント 3

An empty group adds no time.

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。