Problem · Array
Maximum Value for Two Delivery Robots
Learn this problemProblem statement
Houses lie in a row, and values[i] is the delivery value at house i. Two robots must serve non-overlapping contiguous segments of houses.
For this exercise, assume the first robot serves exactly c1 consecutive houses and the second robot serves exactly c2 consecutive houses. Either robot's segment may appear first along the street.
Return the maximum total value served by the two robots.
Function
maxDeliveryValue(values: int[], c1: int, c2: int) → longExamples
Example 1
values = [4,2,3,5]c1 = 2c2 = 1return = 12The second robot serves the first house for value 4. The first robot serves the final two houses for value 3 + 5 = 8. The segments do not overlap, and their total value is 12.
Example 2
values = [1,2,3,4,5]c1 = 2c2 = 2return = 14The robots can serve [2,3] and [4,5]. Their total is 5 + 9 = 14.
Example 3
values = [5,1,1,5]c1 = 1c2 = 1return = 10Each robot serves one of the two houses with value 5, for a total of 10.
Constraints
2 <= values.length <= 2000000 <= values[i] <= 10^91 <= c1and1 <= c2c1 + c2 <= values.length- The answer fits in a signed 64-bit integer.