Best Dispatch Group Total
Context
A logistics company records integer adjustments for each dispatch group leaving its warehouses. A group’s total is the sum of all its adjustments. Some adjustments may be negative, such as delays or damage deductions, while others may be positive. The operations team wants to know the largest group total.
Problem
Write a function that examines every dispatch group and returns the greatest group total. For each inner array, add its values to obtain that group’s total. Compare the completed total with the best total found so far only after the group has been fully processed. If the outer array is empty, return 0. An empty inner array has total 0. Do not assume that a positive total exists: when all non-empty groups have negative totals, return the least negative group total. Process every value once and do not create an array of group totals.
Examples
groups = []0groups = [[4, -1, 3], [2, 2], []]6groups = [[-8, -3], [-2, -5], [-4, -4], [-8, -3]]-7Constraints
0 <= len(groups) <= 1000000 <= len(groups[i]) <= 1000 for every i-1000000 <= groups[i][j] <= 1000000 for every i, j- Types:
groupsis int[][]; result is int
Function signature
def best_dispatch_group_total(groups: list[list[int]]) -> int
groupslist[list[int]]- Dispatch groups, with one integer adjustment array per group.
- returns int
- The greatest sum of any dispatch group, or 0 when there are no groups.
Adapted from MBPP problem task_23 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- An empty outer array returns 0.
- An empty inner array contributes a total of 0.