< Summary

Information
Class: LeetCode.Algorithms.PathWithMaximumGold.PathWithMaximumGoldRecursive
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PathWithMaximumGold\PathWithMaximumGoldRecursive.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 77
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
GetMaximumGold(...)100%66100%
GetMaximumGold(...)100%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PathWithMaximumGold\PathWithMaximumGoldRecursive.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.PathWithMaximumGold;
 13
 14/// <inheritdoc />
 15public class PathWithMaximumGoldRecursive : IPathWithMaximumGold
 16{
 17    /// <summary>
 18    ///     Time complexity - O((m * n) * 4 ^ (m * n)
 19    ///     Space complexity - O(m * n)
 20    /// </summary>
 21    /// <param name="grid"></param>
 22    /// <returns></returns>
 23    public int GetMaximumGold(int[][] grid)
 724    {
 725        var maxGold = 0;
 26
 7427        for (var i = 0; i < grid.Length; i++)
 3028        {
 42229            for (var j = 0; j < grid[i].Length; j++)
 18130            {
 18131                if (grid[i][j] == 0)
 12132                {
 12133                    continue;
 34                }
 35
 6036                maxGold = Math.Max(maxGold, GetMaximumGold(grid, i, j, 0));
 6037            }
 3038        }
 39
 740        return maxGold;
 741    }
 42
 43    private static int GetMaximumGold(IReadOnlyList<int[]> grid, int i, int j, int currentGold)
 102044    {
 102045        var cell = grid[i][j];
 46
 102047        currentGold += cell;
 48
 102049        grid[i][j] = 0;
 50
 102051        var maxGold = currentGold;
 52
 102053        if (i + 1 < grid.Count && grid[i + 1][j] != 0)
 29054        {
 29055            maxGold = Math.Max(maxGold, GetMaximumGold(grid, i + 1, j, currentGold));
 29056        }
 57
 102058        if (i - 1 >= 0 && grid[i - 1][j] != 0)
 33659        {
 33660            maxGold = Math.Max(maxGold, GetMaximumGold(grid, i - 1, j, currentGold));
 33661        }
 62
 102063        if (j + 1 < grid[i].Length && grid[i][j + 1] != 0)
 21164        {
 21165            maxGold = Math.Max(maxGold, GetMaximumGold(grid, i, j + 1, currentGold));
 21166        }
 67
 102068        if (j - 1 >= 0 && grid[i][j - 1] != 0)
 12369        {
 12370            maxGold = Math.Max(maxGold, GetMaximumGold(grid, i, j - 1, currentGold));
 12371        }
 72
 102073        grid[i][j] = cell;
 74
 102075        return maxGold;
 102076    }
 77}