โจทย์
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
ตัวอย่าง
อินพุต:
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.
อินพุต:
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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน