480. Maximum Rocks on a Road Trip
Maximum Rocks on a Road Trip

A rock collector is going on a road trip from Southern California to New York. A rectangular grid shows the number of rare rocks available in each city.

Rows and columns are numbered from 0, starting at the top-left. Let rows and cols be the number of rows and columns.

Start at row rows - 1, column 0, and finish at row 0, column cols - 1. Each move goes one row up or one column right. Collect the rocks in every visited city, including the starting and ending cities. Find the maximum total number of rocks you can collect.

Method Signature

int maximumRocks(List<String> cityRocks)
  • cityRocks: rows from top to bottom. Each string contains the rock counts from left to right, separated by commas, such as "3,0,8".
  • Return the maximum total number of rocks collected along a valid path.

Constraints

  • 1 ≤ cityRocks.size() ≤ 1,000
  • Each row contains between 1 and 1,000 integers, with no spaces. All rows contain the same number of integers.
  • Each rock count is between 0 and 1,000,000, inclusive.

Examples

Example 1

maximumRocks(cityRocks = ["2,1,7", "4,8,0", "3,2,5"])

Output: 23

Move up, right, up, then right to collect 3 + 4 + 8 + 1 + 7 = 23 rocks.

Example 2

maximumRocks(cityRocks = ["1,2,9", "8,0,7"])

Output: 24

Move right, right, then up to collect 8 + 0 + 7 + 9 = 24 rocks.

Example 3

maximumRocks(cityRocks = ["6"])

Output: 6

The starting and ending city are the same, so its rocks are counted once.



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