आसान 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

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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।