< Summary

Information
Class: LeetCode.Algorithms.MaximumNumberOfMovesInGrid.MaximumNumberOfMovesInGridDynamicProgramming
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumNumberOfMovesInGrid\MaximumNumberOfMovesInGridDynamicProgramming.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 70
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaxMoves(...)100%44100%
MaxMoves(...)100%1818100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumNumberOfMovesInGrid\MaximumNumberOfMovesInGridDynamicProgramming.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.MaximumNumberOfMovesInGrid;
 13
 14/// <inheritdoc />
 15public 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)
 224    {
 225        var memoGrid = new int[grid.Length][];
 26
 1827        for (var i = 0; i < grid.Length; i++)
 728        {
 729            memoGrid[i] = new int[grid[i].Length];
 730        }
 31
 232        var maxMoves = 0;
 33
 1834        for (var i = 0; i < grid.Length; i++)
 735        {
 736            maxMoves = Math.Max(maxMoves, MaxMoves(grid, memoGrid, i, 0));
 737        }
 38
 239        return maxMoves;
 240    }
 41
 42    private static int MaxMoves(int[][] grid, int[][] memoGrid, int i, int j)
 1943    {
 1944        var maxMoves = 0;
 45
 1946        if (memoGrid[i][j] != 0)
 447        {
 448            return memoGrid[i][j];
 49        }
 50
 1551        if (i - 1 >= 0 && j + 1 < grid[i].Length && grid[i - 1][j + 1] > grid[i][j])
 252        {
 253            maxMoves = Math.Max(maxMoves, MaxMoves(grid, memoGrid, i - 1, j + 1) + 1);
 254        }
 55
 1556        if (j + 1 < grid[i].Length && grid[i][j + 1] > grid[i][j])
 557        {
 558            maxMoves = Math.Max(maxMoves, MaxMoves(grid, memoGrid, i, j + 1) + 1);
 559        }
 60
 1561        if (i + 1 < grid.Length && j + 1 < grid[i].Length && grid[i + 1][j + 1] > grid[i][j])
 562        {
 563            maxMoves = Math.Max(maxMoves, MaxMoves(grid, memoGrid, i + 1, j + 1) + 1);
 564        }
 65
 1566        memoGrid[i][j] = maxMoves;
 67
 1568        return maxMoves;
 1969    }
 70}