The Indian Engineer

Problem 2684 Maximum Number of Moves in a Grid

Posted on 7 mins

Array Dynamic-Programming Matrix

Problem Statement

Link - Problem 2684

Question

You are given a 0-indexed m x n matrix grid consisting of positive integers.

You can start at any cell in the first column of the matrix, and traverse the grid in the following way:

Return the maximum number of moves that you can perform.

Example 1:

Input: grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]
Output: 3
Explanation: We can start at the cell (0, 0) and make the following moves:
- (0, 0) -> (0, 1).
- (0, 1) -> (1, 2).
- (1, 2) -> (2, 3).
It can be shown that it is the maximum number of moves that can be made.

Example 2:


Input: grid = [[3,2,4],[2,1,9],[1,1,7]]
Output: 0
Explanation: Starting from any cell in the first column we cannot perform any moves.

Constraints:

Solution

class Solution {
public:
    int maxMoves(vector<vector<int>>& grid) {
        int rows = grid.size();
        int cols = grid[0].size();
        int answer = 0;

        vector<vector<int>> dp(rows, vector<int>(cols, 0));

        for (int col = cols - 1; col >= 0; col--) {
            for (int row = 0; row < rows; row++) {
                int diag_up = 0, right = 0, diag_down = 0;

                if (row - 1 >= 0 && col + 1 < cols && grid[row - 1][col + 1] > grid[row][col]) {
                    diag_up = 1 + dp[row - 1][col + 1];
                }

                if (col + 1 < cols && grid[row][col + 1] > grid[row][col]) {
                    right = 1 + dp[row][col + 1];
                }

                if (row + 1 < rows && col + 1 < cols && grid[row + 1][col + 1] > grid[row][col]) {
                    diag_down = 1 + dp[row + 1][col + 1];
                }

                dp[row][col] = max(diag_up, max(right, diag_down));

                if (col == 0) {
                    answer = max(answer, dp[row][0]);
                }
            }
        }

        return answer;
    }
};

Complexity Analysis

| Algorithm           | Time Complexity | Space Complexity |
| ------------------- | --------------- | ---------------- |
| Dynamic Programming | O(nm)           | O(nm)            |

Explanation

Intial Thoughts

To start, think of the grid as a maze where each cell has a specific value. Consider that you can only move right, and in order to do so, you must find a cell with a higher value than the current one. Think about how you can use this constraint to limit the possible moves. Consider starting from the last column and working your way back to the first, keeping track of the maximum number of moves you can make from each cell. Use this information to narrow down the possible paths and find the longest one.

Intuitive Analysis

Imagine each cell in the grid as a mountain with a certain height, where you can only climb to a higher mountain. From each mountain, you have three options to move to the next column: up and to the right, directly to the right, or down and to the right. You can only take a step if the next mountain is higher, so you need to keep track of the maximum height you can reach from each mountain. By working backwards from the last column, you can build a map of the maximum heights, which will help you find the longest path from the first column to the last.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Consider using dynamic programming to find the maximum number of moves that can be made from each cell.

The final answer will be the maximum value in cells of the first column.