475. Minimum Starting Charge for a Warehouse Robot
Minimum Starting Charge for a Warehouse Robot

A warehouse robot visits a rectangular grid of workstations. It starts at the top-left workstation and must reach the bottom-right workstation. Each move takes it one workstation right or down.

Each workstation changes the battery level. A negative value consumes battery units, a positive value adds battery units, and zero leaves it unchanged.

Apply the change at every visited workstation, including the first and last. Moving has no additional battery cost. The battery has no maximum charge limit and must always remain at 1 unit or more, including after the final workstation.

Method Signature

int minimumStartingCharge(List<String> chargeChanges)
  • chargeChanges: rows listed from top to bottom. Each string contains the row's battery changes from left to right, separated by commas.
  • Return the minimum starting charge needed for at least one valid route, measured before applying the first workstation's change.

Constraints

  • 1 ≤ chargeChanges.size() ≤ 200
  • Each row contains between 1 and 200 valid integers, separated by commas without spaces.
  • All rows contain the same number of integers.
  • Each integer is between -1,000 and 1,000, inclusive.
  • The starting charge must be a positive integer.

Examples

Example 1

Call: minimumStartingCharge(chargeChanges = ["-2,-5,9", "1,-4,-3"])

Output: 8

Choose right → right → down. Starting with 8 units, the battery levels become 6 → 1 → 10 → 7. No route works with a smaller starting charge.

Example 2

Call: minimumStartingCharge(chargeChanges = ["2,-7", "-4,-2"])

Output: 5

Choose down → right. Starting with 5 units, the battery levels become 7 → 3 → 1. Any smaller starting charge fails on every route.

Example 3

Call: minimumStartingCharge(chargeChanges = ["7"])

Output: 1

The only workstation adds 7 units. Starting with 1 unit is enough, leaving 8 units after the change.



Please use Laptop/Desktop or any other large screen to add/edit code.