PracticeArrays

Best Dispatch Group Total

Easy/MediumArraystraversalaccumulationstate tracking Time not estimated Not started

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

Example 1
Input: groups = []
Output: 0
Explanation: The executed call with no groups returns 0.
Example 2
Input: groups = [[4, -1, 3], [2, 2], []]
Output: 6
Explanation: The executed call with groups [[4, -1, 3], [2, 2], []] returns 6.
Example 3
Input: groups = [[-8, -3], [-2, -5], [-4, -4], [-8, -3]]
Output: -7
Explanation: The executed call with four negative-total groups returns -7.

Constraints

  • 0 <= len(groups) <= 100000
  • 0 <= len(groups[i]) <= 1000 for every i
  • -1000000 <= groups[i][j] <= 1000000 for every i, j
  • Types: groups is int[][]; result is int

Function signature

def best_dispatch_group_total(groups: list[list[int]]) -> int
groups list[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.
Approved · cb006r1-abs_477_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1