| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.MaximumNumberOfMovesInGrid; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MaximumNumberOfMovesInGridDynamicProgramming : IMaximumNumberOfMovesInGrid |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n * m), where n is number of rows and m is the number of columns |
| | 19 | | /// Space complexity - O(n), where n is number of rows |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="grid"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int MaxMoves(int[][] grid) |
| 2 | 24 | | { |
| 2 | 25 | | var memoGrid = new int[grid.Length][]; |
| | 26 | |
|
| 18 | 27 | | for (var i = 0; i < grid.Length; i++) |
| 7 | 28 | | { |
| 7 | 29 | | memoGrid[i] = new int[grid[i].Length]; |
| 7 | 30 | | } |
| | 31 | |
|
| 2 | 32 | | var maxMoves = 0; |
| | 33 | |
|
| 18 | 34 | | for (var i = 0; i < grid.Length; i++) |
| 7 | 35 | | { |
| 7 | 36 | | maxMoves = Math.Max(maxMoves, MaxMoves(grid, memoGrid, i, 0)); |
| 7 | 37 | | } |
| | 38 | |
|
| 2 | 39 | | return maxMoves; |
| 2 | 40 | | } |
| | 41 | |
|
| | 42 | | private static int MaxMoves(int[][] grid, int[][] memoGrid, int i, int j) |
| 19 | 43 | | { |
| 19 | 44 | | var maxMoves = 0; |
| | 45 | |
|
| 19 | 46 | | if (memoGrid[i][j] != 0) |
| 4 | 47 | | { |
| 4 | 48 | | return memoGrid[i][j]; |
| | 49 | | } |
| | 50 | |
|
| 15 | 51 | | if (i - 1 >= 0 && j + 1 < grid[i].Length && grid[i - 1][j + 1] > grid[i][j]) |
| 2 | 52 | | { |
| 2 | 53 | | maxMoves = Math.Max(maxMoves, MaxMoves(grid, memoGrid, i - 1, j + 1) + 1); |
| 2 | 54 | | } |
| | 55 | |
|
| 15 | 56 | | if (j + 1 < grid[i].Length && grid[i][j + 1] > grid[i][j]) |
| 5 | 57 | | { |
| 5 | 58 | | maxMoves = Math.Max(maxMoves, MaxMoves(grid, memoGrid, i, j + 1) + 1); |
| 5 | 59 | | } |
| | 60 | |
|
| 15 | 61 | | if (i + 1 < grid.Length && j + 1 < grid[i].Length && grid[i + 1][j + 1] > grid[i][j]) |
| 5 | 62 | | { |
| 5 | 63 | | maxMoves = Math.Max(maxMoves, MaxMoves(grid, memoGrid, i + 1, j + 1) + 1); |
| 5 | 64 | | } |
| | 65 | |
|
| 15 | 66 | | memoGrid[i][j] = maxMoves; |
| | 67 | |
|
| 15 | 68 | | return maxMoves; |
| 19 | 69 | | } |
| | 70 | | } |