Maximum Strictly Increasing Cells in a Matrix - Problem

Imagine you're a treasure hunter exploring a mysterious grid-based dungeon where each cell contains a magical number. You can start from any cell and move to other cells in the same row or column, but there's a catch: you can only move to cells with strictly greater values than your current position!

Given an m x n integer matrix, your goal is to find the maximum number of cells you can visit by choosing the optimal starting position and making the smartest moves possible.

Movement Rules:

  • Start from any cell in the matrix
  • Move only within the same row or column
  • Destination cell value must be strictly greater than current cell
  • Continue until no valid moves remain

Goal: Return the maximum number of cells that can be visited in a single journey.

Input & Output

example_1.py — Basic Matrix
$ Input: mat = [[3,1,6],[-9,5,7]]
Output: 4
💡 Note: The optimal path is: -9 → 5 → 7 → 6. Start at (-9) in position (1,0), move to (5) in same row at (1,1), then move to (7) in same row at (1,2), finally move to (6) in same column at (0,2). This gives us a path of length 4.
example_2.py — Single Row
$ Input: mat = [[7,6,3]]
Output: 1
💡 Note: Since all movements must be to strictly greater values, and this row is in decreasing order, we can only visit one cell. No matter which cell we start from, there are no valid moves to cells with greater values.
example_3.py — Perfect Increasing
$ Input: mat = [[1,2],[3,4]]
Output: 3
💡 Note: Multiple optimal paths exist: 1→2→4 or 1→3→4, both having length 3. We can start at (1) and move to either (2) or (3), then continue to (4) for a total of 3 cells visited.

Constraints

  • m == mat.length
  • n == mat[i].length
  • 1 ≤ m, n ≤ 105
  • 1 ≤ m * n ≤ 105
  • -105 ≤ mat[i][j] ≤ 105

Visualization

Tap to expand
Maximum Strictly Increasing Cells in a Matrix INPUT 2x3 Matrix Grid 3 1 6 -9 5 7 [0,0] [0,1] [0,2] mat = [[3,1,6], [-9,5,7]] Movement Rules: Same row OR column Strictly increasing values Start anywhere ALGORITHM STEPS 1 Sort Cells by Value -9, 1, 3, 5, 6, 7 2 Dynamic Programming dp[i][j] = max cells from (i,j) 3 Track Row/Col Max rowMax[], colMax[] 4 Process Small to Large Update dp with best path Optimal Path Found: -9 5 6 7 -9 --> 5 --> 6 --> 7 (same column then row) FINAL RESULT Path Through Matrix 3 1 6 3 -9 1 5 2 7 4 Output: 4 OK - Maximum cells = 4 Start: (-9) at [1,0] End: (7) at [1,2] 4 strictly increasing cells Key Insight: Sort cells by value and use DP to track maximum reachable cells for each row and column. For each cell, dp[i][j] = max(rowMax[i], colMax[j]) + 1. Process smaller values first to ensure we only consider valid increasing paths. Time: O(mn log(mn)), Space: O(mn). TutorialsPoint - Maximum Strictly Increasing Cells in a Matrix | Optimal Solution
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
34.3K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen