Problem · Dynamic Programming
Matrix Traversal
Given a 4 x 4 matrix mat, the initial energy is 100. The task is to reach the last row of the matrix with the maximum possible energy left.
The matrix can be traversed in the following way:
After stepping on a cell (i, j), energy decreases by mat[i][j] units. Find the maximum possible energy left at the end of the traversal.
Note: The final energy can be negative.
Complete the function maxEnergy in the editor below.
maxEnergy has the following parameter:
int mat[4][4]: a matrix of integers
Returns
int: the maximum possible energy at the end of the traversal
Examples
01 · Example 1
mat = [[10, 20, 30, 40], [60, 50, 20, 80], [10, 10, 10, 10], [60, 50, 60, 50]] return = 0
Possible paths
(0-based indexing is used):
- (0, 0) - (1, 1) - (2, 2) - (3, 3)
- (0, 1) - (1, 2) - (2, 2) - (3, 2)
Constraints
0 <= mat[i][j] < 100More Flexport problems
public int maxEnergy(int[][] mat) {
// write your code here
}
mat[[10, 20, 30, 40], [60, 50, 20, 80], [10, 10, 10, 10], [60, 50, 60, 50]]
expected0
sign in to submit