Counting the best paths
Write code · Challenge · §10.3 Two-dimensional grid tables
The map of a live-stream mini game is a grid with a number of hearts (possibly 0) on each cell. hearts[i][j] is the number of hearts on cell j from the left in row i from the top. Rows and columns are numbered from 0.
The character starts at the top-left cell and moves one cell at a time, only right or down, until it reaches the bottom-right cell, collecting the hearts on every cell it passes through, including the starting cell and the goal cell. Complete the function solution, which returns the number of paths that collect the most hearts. Two paths are different if at least one cell they pass through is different. If several paths collect that largest number of hearts, count all of them. If the map has only one cell, there is one path that does not move.
Function
solution(hearts)
Constraints
- The length of
hearts(the number of rows) is from 1 to 30. - Every element of
heartsis a list of the same length (the number of cells in a row), from 1 to 30. - Each number of hearts is an integer from 0 to 9.
- The answer can be a very large number. Return it as a Python integer as it is, without taking a remainder.
Examples
| hearts | result |
|---|---|
[[1, 2], [3, 1]] | 1 |
[[2, 2, 9], [2, 6, 2], [1, 9, 3]] | 2 |
Starter code
def solution(hearts):
answer = 0
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.