890. Best Score Board Paths

Hard7.5 points estimatedMatrixDynamic Programming3 hints

A delivery robot works on a warehouse floor described by board, a list of equal-length strings. It starts on the top-left cell, marked S, and must reach the bottom-right cell, marked E. From a cell it may step one cell right, one cell down, or one cell diagonally down-right, and it may never enter a cell marked #.

Every other cell holds a lowercase letter from a to i standing for a parcel worth 1 to 9 points (a = 1, b = 2, ..., i = 9); S and E are worth 0. A route scores the sum of the cells it visits. Return [best, ways]: the highest score of any route and the number of distinct routes reaching that score, taken modulo 10^9 + 7. If E cannot be reached, return [0, 0].

Example 1

Input:
board = ["Sdab","b#dc","cacE"]
Output:
[12,2]
Explanation:

Two routes tie for the top score 12: Sdadc then E, reached either through the cell holding c in the second row's last column or through the c in the bottom row; so the answer is [12, 2].

Example 2

Input:
board = ["Sddb","#dba","#aaE"]
Output:
[11,5]
Explanation:

The best score is 11 and five different routes reach it, so the answer is [11, 5].

Example 3

Input:
board = ["S#db","##dc","bbcE"]
Output:
[0,0]
Explanation:

Every cell next to S is blocked, so E cannot be reached and the answer is [0, 0].

Constraints

  • 1 ≤ rows, columns ≤ 100 and rows * columns ≥ 2
  • board[0][0] is S, the last cell of the last row is E, and no other cell is S or E
  • Every other cell is # or a letter from a to i

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms

Expected complexity

Time
O(R * C)
Space
O(R * C)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…