Matrix LeetCode Problems: All 216, With Python Solutions
Every problem in this library that LeetCode tags Matrix — 216 in total, 178 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 216 problems
- 32 Easy
- 125 Medium
- 59 Hard
How Matrix problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Matrix problems — each one explains the approach, gives a Python template and states its complexity.
- Matrix and Grid — Treat a 2-D grid as a graph whose neighbours are the four adjacent cells.
Matrix problems by difficulty
Showing the first 200 of 216 problems. Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (32)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 463 | Island Perimeter | Easy | Depth-First Search, Breadth-First Search, Array +1 |
| 566 | Reshape the Matrix | Easy | Array, Matrix, Simulation |
| 661 | Image Smoother | Easy | Array, Matrix |
| 733 | Flood Fill | Easy | Depth-First Search, Breadth-First Search, Array +1 |
| 766 | Toeplitz Matrix | Easy | Array, Matrix |
| 832 | Flipping an Image | Easy | Bit Manipulation, Array, Two Pointers +2 |
| 867 | Transpose Matrix | Easy | Array, Matrix, Simulation |
| 883 | Projection Area of 3D Shapes | Easy | Geometry, Array, Math +1 |
| 892 | Surface Area of 3D Shapes | Easy | Geometry, Array, Math +1 |
| 999 | Available Captures for Rook | Easy | Array, Matrix, Simulation |
| 1030 | Matrix Cells in Distance Order | Easy | Geometry, Array, Math +2 |
| 1260 | Shift 2D Grid | Easy | Array, Matrix, Simulation |
| 1275 | Find Winner on a Tic Tac Toe Game | Easy | Array, Hash Table, Matrix +1 |
| 1337 | The K Weakest Rows in a Matrix | Easy | Array, Binary Search, Matrix +2 |
| 1351 | Count Negative Numbers in a Sorted Matrix | Easy | Array, Binary Search, Matrix |
| 1380 | Lucky Numbers in a Matrix | Easy | Array, Matrix |
| 1572 | Matrix Diagonal Sum | Easy | Array, Matrix |
| 1582 | Special Positions in a Binary Matrix | Easy | Array, Matrix |
| 1672 | Richest Customer Wealth | Easy | Array, Matrix |
| 1886 | Determine Whether Matrix Can Be Obtained By Rotation | Easy | Array, Matrix |
| 2022 | Convert 1D Array Into 2D Array | Easy | Array, Matrix, Simulation |
| 2133 | Check if Every Row and Column Contains All Numbers | Easy | Array, Hash Table, Matrix |
| 2319 | Check if Matrix Is X-Matrix | Easy | Array, Matrix |
| 2373 | Largest Local Values in a Matrix | Easy | Array, Matrix |
| 2500 | Delete Greatest Value in Each Row | Easy | Array, Matrix, Sorting +2 |
| 2614 | Prime In Diagonal | Easy | Array, Math, Matrix +1 |
| 2639 | Find the Width of Columns of a Grid | Easy | Array, Matrix |
| 2643 | Row With Maximum Ones | Easy | Array, Matrix |
| 2923 | Find Champion I | Easy | Array, Matrix |
| 2946 | Matrix Similarity After Cyclic Shifts | Easy | Array, Math, Matrix +1 |
| 2965 | Find Missing and Repeated Values | Easy | Array, Hash Table, Math +1 |
| 422 | Valid Word SquarePremium | Easy | Array, Matrix |
Medium (113)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 36 | Valid Sudoku | Medium | Array, Hash Table, Matrix |
| 48 | Rotate Image | Medium | Array, Math, Matrix |
| 54 | Spiral Matrix | Medium | Array, Matrix, Simulation |
| 59 | Spiral Matrix II | Medium | Array, Matrix, Simulation |
| 63 | Unique Paths II | Medium | Array, Dynamic Programming, Matrix |
| 64 | Minimum Path Sum | Medium | Array, Dynamic Programming, Matrix |
| 73 | Set Matrix Zeroes | Medium | Array, Hash Table, Matrix |
| 74 | Search a 2D Matrix | Medium | Array, Binary Search, Matrix |
| 79 | Word Search | Medium | Depth-First Search, Array, String +2 |
| 130 | Surrounded Regions | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 200 | Number of Islands | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 221 | Maximal Square | Medium | Array, Dynamic Programming, Matrix |
| 240 | Search a 2D Matrix II | Medium | Array, Binary Search, Divide and Conquer +1 |
| 289 | Game of Life | Medium | Array, Matrix, Simulation |
| 304 | Range Sum Query 2D - Immutable | Medium | Design, Array, Matrix +1 |
| 378 | Kth Smallest Element in a Sorted Matrix | Medium | Array, Binary Search, Matrix +2 |
| 417 | Pacific Atlantic Water Flow | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 419 | Battleships in a Board | Medium | Depth-First Search, Array, Matrix |
| 427 | Construct Quad Tree | Medium | Tree, Array, Divide and Conquer +1 |
| 498 | Diagonal Traverse | Medium | Array, Matrix, Simulation |
| 529 | Minesweeper | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 542 | 01 Matrix | Medium | Breadth-First Search, Array, Dynamic Programming +1 |
| 695 | Max Area of Island | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 794 | Valid Tic-Tac-Toe State | Medium | Array, Matrix |
| 807 | Max Increase to Keep City Skyline | Medium | Greedy, Array, Matrix |
| 835 | Image Overlap | Medium | Array, Matrix |
| 840 | Magic Squares In Grid | Medium | Array, Hash Table, Math +1 |
| 861 | Score After Flipping Matrix | Medium | Greedy, Bit Manipulation, Array +1 |
| 885 | Spiral Matrix III | Medium | Array, Matrix, Simulation |
| 909 | Snakes and Ladders | Medium | Breadth-First Search, Array, Matrix |
| 931 | Minimum Falling Path Sum | Medium | Array, Dynamic Programming, Matrix |
| 934 | Shortest Bridge | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 959 | Regions Cut By Slashes | Medium | Depth-First Search, Breadth-First Search, Union Find +3 |
| 994 | Rotting Oranges | Medium | Breadth-First Search, Array, Matrix |
| 1020 | Number of Enclaves | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 1034 | Coloring A Border | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 1072 | Flip Columns For Maximum Number of Equal Rows | Medium | Array, Hash Table, Matrix |
| 1091 | Shortest Path in Binary Matrix | Medium | Breadth-First Search, Array, Matrix |
| 1139 | Largest 1-Bordered Square | Medium | Array, Dynamic Programming, Matrix |
| 1162 | As Far from Land as Possible | Medium | Breadth-First Search, Array, Dynamic Programming +1 |
| 1219 | Path with Maximum Gold | Medium | Array, Backtracking, Matrix |
| 1222 | Queens That Can Attack the King | Medium | Array, Matrix, Simulation |
| 1253 | Reconstruct a 2-Row Binary Matrix | Medium | Greedy, Array, Matrix |
| 1254 | Number of Closed Islands | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 1267 | Count Servers that Communicate | Medium | Depth-First Search, Breadth-First Search, Union Find +3 |
| 1277 | Count Square Submatrices with All Ones | Medium | Array, Dynamic Programming, Matrix |
| 1292 | Maximum Side Length of a Square with Sum Less than or Equal to Threshold | Medium | Array, Binary Search, Matrix +1 |
| 1314 | Matrix Block Sum | Medium | Array, Matrix, Prefix Sum |
| 1329 | Sort the Matrix Diagonally | Medium | Array, Matrix, Sorting |
| 1391 | Check if There is a Valid Path in a Grid | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 1476 | Subrectangle Queries | Medium | Design, Array, Matrix |
| 1504 | Count Submatrices With All Ones | Medium | Stack, Array, Dynamic Programming +2 |
| 1536 | Minimum Swaps to Arrange a Binary Grid | Medium | Greedy, Array, Matrix |
| 1559 | Detect Cycles in 2D Grid | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 1594 | Maximum Non Negative Product in a Matrix | Medium | Array, Dynamic Programming, Matrix |
| 1605 | Find Valid Matrix Given Row and Column Sums | Medium | Greedy, Array, Matrix |
| 1631 | Path With Minimum Effort | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1706 | Where Will the Ball Fall | Medium | Array, Matrix, Simulation |
| 1727 | Largest Submatrix With Rearrangements | Medium | Greedy, Array, Matrix +1 |
| 1738 | Find Kth Largest XOR Coordinate Value | Medium | Bit Manipulation, Array, Divide and Conquer +5 |
| 1765 | Map of Highest Peak | Medium | Breadth-First Search, Array, Matrix |
| 1861 | Rotating the Box | Medium | Array, Two Pointers, Matrix |
| 1878 | Get Biggest Three Rhombus Sums in a Grid | Medium | Array, Math, Matrix +3 |
| 1895 | Largest Magic Square | Medium | Array, Matrix, Prefix Sum |
| 1901 | Find a Peak Element II | Medium | Array, Binary Search, Matrix |
| 1905 | Count Sub Islands | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 1914 | Cyclically Rotating a Grid | Medium | Array, Matrix, Simulation |
| 1926 | Nearest Exit from Entrance in Maze | Medium | Breadth-First Search, Array, Matrix |
| 1937 | Maximum Number of Points with Cost | Medium | Array, Dynamic Programming, Matrix |
| 1958 | Check if Move is Legal | Medium | Array, Enumeration, Matrix |
| 1975 | Maximum Matrix Sum | Medium | Greedy, Array, Matrix |
| 1981 | Minimize the Difference Between Target and Chosen Elements | Medium | Array, Dynamic Programming, Matrix |
| 1992 | Find All Groups of Farmland | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 2017 | Grid Game | Medium | Array, Matrix, Prefix Sum |
| 2018 | Check if Word Can Be Placed In Crossword | Medium | Array, Enumeration, Matrix |
| 2033 | Minimum Operations to Make a Uni-Value Grid | Medium | Array, Math, Matrix +1 |
| 2125 | Number of Laser Beams in a Bank | Medium | Array, Math, String +1 |
| 2146 | K Highest Ranked Items Within a Price Range | Medium | Breadth-First Search, Array, Matrix +2 |
| 2245 | Maximum Trailing Zeros in a Cornered Path | Medium | Array, Matrix, Prefix Sum |
| 2257 | Count Unguarded Cells in the Grid | Medium | Array, Matrix, Simulation |
| 2304 | Minimum Path Cost in a Grid | Medium | Array, Dynamic Programming, Matrix |
| 2326 | Spiral Matrix IV | Medium | Array, Linked List, Matrix +1 |
| 2352 | Equal Row and Column Pairs | Medium | Array, Hash Table, Matrix +1 |
| 2397 | Maximum Rows Covered by Columns | Medium | Bit Manipulation, Array, Backtracking +2 |
| 2428 | Maximum Sum of an Hourglass | Medium | Array, Matrix, Prefix Sum |
| 2482 | Difference Between Ones and Zeros in Row and Column | Medium | Array, Matrix, Simulation |
| 2536 | Increment Submatrices by One | Medium | Array, Matrix, Prefix Sum |
| 2545 | Sort the Students by Their Kth Score | Medium | Array, Matrix, Sorting |
| 2556 | Disconnect Path in a Binary Matrix by at Most One Flip | Medium | Depth-First Search, Breadth-First Search, Array +2 |
| 2596 | Check Knight Tour Configuration | Medium | Depth-First Search, Breadth-First Search, Array +2 |
| 2658 | Maximum Number of Fish in a Grid | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 2661 | First Completely Painted Row or Column | Medium | Array, Hash Table, Matrix |
| 2679 | Sum in a Matrix | Medium | Array, Matrix, Sorting +2 |
| 2684 | Maximum Number of Moves in a Grid | Medium | Array, Dynamic Programming, Matrix |
| 2711 | Difference of Number of Distinct Values on Diagonals | Medium | Array, Hash Table, Matrix |
| 2812 | Find the Safest Path in a Grid | Medium | Breadth-First Search, Union Find, Array +3 |
| 2850 | Minimum Moves to Spread Stones Over Grid | Medium | Breadth-First Search, Array, Dynamic Programming +1 |
| 2906 | Construct Product Matrix | Medium | Array, Matrix, Prefix Sum |
| 286 | Walls and GatesPremium | Medium | Breadth-First Search, Array, Matrix |
| 308 | Range Sum Query 2D - MutablePremium | Medium | Design, Binary Indexed Tree, Segment Tree +2 |
| 311 | Sparse Matrix MultiplicationPremium | Medium | Array, Hash Table, Matrix |
| 348 | Design Tic-Tac-ToePremium | Medium | Design, Array, Hash Table +2 |
| 361 | Bomb EnemyPremium | Medium | Array, Dynamic Programming, Matrix |
| 490 | The MazePremium | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 505 | The Maze IIPremium | Medium | Depth-First Search, Breadth-First Search, Graph +4 |
| 531 | Lonely Pixel IPremium | Medium | Array, Hash Table, Matrix |
| 533 | Lonely Pixel IIPremium | Medium | Array, Hash Table, Matrix |
| 562 | Longest Line of Consecutive One in MatrixPremium | Medium | Array, Dynamic Programming, Matrix |
| 723 | Candy CrushPremium | Medium | Array, Two Pointers, Matrix +1 |
| 750 | Number Of Corner RectanglesPremium | Medium | Array, Math, Dynamic Programming +1 |
| 1102 | Path With Maximum Minimum ValuePremium | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1198 | Find Smallest Common Element in All RowsPremium | Medium | Array, Hash Table, Binary Search +2 |
| 1428 | Leftmost Column with at Least a OnePremium | Medium | Array, Binary Search, Interactive +1 |
Hard (55)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 37 | Sudoku Solver | Hard | Array, Hash Table, Backtracking +1 |
| 85 | Maximal Rectangle | Hard | Stack, Array, Dynamic Programming +2 |
| 174 | Dungeon Game | Hard | Array, Dynamic Programming, Matrix |
| 212 | Word Search II | Hard | Trie, Array, String +2 |
| 329 | Longest Increasing Path in a Matrix | Hard | Depth-First Search, Breadth-First Search, Graph +5 |
| 363 | Max Sum of Rectangle No Larger Than K | Hard | Array, Binary Search, Matrix +2 |
| 407 | Trapping Rain Water II | Hard | Breadth-First Search, Array, Matrix +1 |
| 675 | Cut Off Trees for Golf Event | Hard | Breadth-First Search, Array, Matrix +1 |
| 741 | Cherry Pickup | Hard | Array, Dynamic Programming, Matrix |
| 749 | Contain Virus | Hard | Depth-First Search, Breadth-First Search, Array +2 |
| 773 | Sliding Puzzle | Hard | Breadth-First Search, Memoization, Array +3 |
| 778 | Swim in Rising Water | Hard | Depth-First Search, Breadth-First Search, Union Find +4 |
| 782 | Transform to Chessboard | Hard | Bit Manipulation, Array, Math +1 |
| 803 | Bricks Falling When Hit | Hard | Union Find, Array, Matrix |
| 827 | Making A Large Island | Hard | Depth-First Search, Breadth-First Search, Union Find +2 |
| 864 | Shortest Path to Get All Keys | Hard | Bit Manipulation, Breadth-First Search, Array +1 |
| 980 | Unique Paths III | Hard | Bit Manipulation, Array, Backtracking +1 |
| 1074 | Number of Submatrices That Sum to Target | Hard | Array, Hash Table, Matrix +1 |
| 1210 | Minimum Moves to Reach Target with Rotations | Hard | Breadth-First Search, Array, Matrix |
| 1263 | Minimum Moves to Move a Box to Their Target Location | Hard | Breadth-First Search, Array, Matrix +1 |
| 1284 | Minimum Number of Flips to Convert Binary Matrix to Zero Matrix | Hard | Bit Manipulation, Breadth-First Search, Array +2 |
| 1289 | Minimum Falling Path Sum II | Hard | Array, Dynamic Programming, Matrix |
| 1293 | Shortest Path in a Grid with Obstacles Elimination | Hard | Breadth-First Search, Array, Matrix |
| 1301 | Number of Paths with Max Score | Hard | Array, Dynamic Programming, Matrix |
| 1349 | Maximum Students Taking Exam | Hard | Bit Manipulation, Array, Dynamic Programming +2 |
| 1368 | Minimum Cost to Make at Least One Valid Path in a Grid | Hard | Breadth-First Search, Graph, Array +3 |
| 1439 | Find the Kth Smallest Sum of a Matrix With Sorted Rows | Hard | Array, Binary Search, Matrix +1 |
| 1444 | Number of Ways of Cutting a Pizza | Hard | Memoization, Array, Dynamic Programming +2 |
| 1463 | Cherry Pickup II | Hard | Array, Dynamic Programming, Matrix |
| 1568 | Minimum Number of Days to Disconnect Island | Hard | Depth-First Search, Breadth-First Search, Array +2 |
| 1595 | Minimum Cost to Connect Two Groups of Points | Hard | Bit Manipulation, Array, Dynamic Programming +2 |
| 1632 | Rank Transform of a Matrix | Hard | Union Find, Graph, Topological Sort +3 |
| 1728 | Cat and Mouse II | Hard | Graph, Topological Sort, Memoization +5 |
| 1970 | Last Day Where You Can Still Cross | Hard | Depth-First Search, Breadth-First Search, Union Find +3 |
| 2088 | Count Fertile Pyramids in a Land | Hard | Array, Dynamic Programming, Matrix |
| 2132 | Stamping the Grid | Hard | Greedy, Array, Matrix +1 |
| 2258 | Escape the Spreading Fire | Hard | Breadth-First Search, Array, Binary Search +1 |
| 2267 | Check if There Is a Valid Parentheses String Path | Hard | Array, Dynamic Programming, Matrix |
| 2290 | Minimum Obstacle Removal to Reach Corner | Hard | Breadth-First Search, Graph, Array +3 |
| 2328 | Number of Increasing Paths in a Grid | Hard | Depth-First Search, Breadth-First Search, Graph +5 |
| 2392 | Build a Matrix With Conditions | Hard | Graph, Topological Sort, Array +1 |
| 2435 | Paths in Matrix Whose Sum Is Divisible by K | Hard | Array, Dynamic Programming, Matrix |
| 2503 | Maximum Number of Points From Grid Queries | Hard | Breadth-First Search, Union Find, Array +4 |
| 2573 | Find the String with LCP | Hard | Greedy, Union Find, Array +3 |
| 2577 | Minimum Time to Visit a Cell In a Grid | Hard | Breadth-First Search, Graph, Array +3 |
| 2617 | Minimum Number of Visited Cells in a Grid | Hard | Stack, Breadth-First Search, Union Find +5 |
| 2713 | Maximum Strictly Increasing Cells in a Matrix | Hard | Memoization, Array, Hash Table +5 |
| 2732 | Find a Good Subset of the Matrix | Hard | Bit Manipulation, Array, Hash Table +1 |
| 2931 | Maximum Spending After Buying Items | Hard | Greedy, Array, Matrix +2 |
| 296 | Best Meeting PointPremium | Hard | Array, Math, Matrix +1 |
| 302 | Smallest Rectangle Enclosing Black PixelsPremium | Hard | Depth-First Search, Breadth-First Search, Array +2 |
| 317 | Shortest Distance from All BuildingsPremium | Hard | Breadth-First Search, Array, Matrix |
| 499 | The Maze IIIPremium | Hard | Depth-First Search, Breadth-First Search, Graph +5 |
| 568 | Maximum Vacation DaysPremium | Hard | Array, Dynamic Programming, Matrix |
| 631 | Design Excel Sum FormulaPremium | Hard | Graph, Design, Topological Sort +4 |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Greedy346
- Depth-First Search289
- Binary Search253
- Database249
- Tree225
- Breadth-First Search223
- Two Pointers201
- Bit Manipulation194
- Binary Tree174
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
- Backtracking105
When the Matrix problem arrives live
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.