Problem · Array

Maximum Value for Two Delivery Robots

Learn this problem
MediumNuro logoNuroFULLTIMEPHONE SCREEN

Problem 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) → long

Examples

Example 1

values = [4,2,3,5]c1 = 2c2 = 1return = 12

The 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 = 14

The robots can serve [2,3] and [4,5]. Their total is 5 + 9 = 14.

Example 3

values = [5,1,1,5]c1 = 1c2 = 1return = 10

Each robot serves one of the two houses with value 5, for a total of 10.

Constraints

  • 2 <= values.length <= 200000
  • 0 <= values[i] <= 10^9
  • 1 <= c1 and 1 <= c2
  • c1 + c2 <= values.length
  • The answer fits in a signed 64-bit integer.
drafts saved locally
public long maxDeliveryValue(int[] values, int c1, int c2) {
    // Write your code here.
}
values[4,2,3,5]
c12
c21
expected12
checking account