Dễ 0 đã giải Tỷ lệ chấp nhận 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.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

0 ≤ number of groups ≤ 10,000

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

Ví dụ

Ví dụ 1
Đầu vào:
groups = [
    [3, 1, 2],
    [5],
    [2, 2]
]

Kết quả mong đợi:
expected = 10
Đầu vào:
std::vector<std::vector<long long>> groups = {
    {3, 1, 2},
    {5},
    {2, 2}
};

Kết quả mong đợi:
long long expected = 10;
Đầu vào:
long[][] groups = new long[][] {
    new long[] {3, 1, 2},
    new long[] {5},
    new long[] {2, 2}
};

Kết quả mong đợi:
long expected = 10;

Giải thích

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

Ví dụ 2
Đầu vào:
groups = [
    [],
    [4, 7]
]

Kết quả mong đợi:
expected = 7
Đầu vào:
std::vector<std::vector<long long>> groups = {
    {},
    {4, 7}
};

Kết quả mong đợi:
long long expected = 7;
Đầu vào:
long[][] groups = new long[][] {
    new long[] {},
    new long[] {4, 7}
};

Kết quả mong đợi:
long expected = 7;

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

A group finishes when its slowest task finishes.

Gợi ý 2

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

Gợi ý 3

An empty group adds no time.

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.