모든 챌린지
쉬움 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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.