| | 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.PathWithMaximumGold; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public 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) |
| 7 | 24 | | { |
| 7 | 25 | | var maxGold = 0; |
| | 26 | |
|
| 74 | 27 | | for (var i = 0; i < grid.Length; i++) |
| 30 | 28 | | { |
| 422 | 29 | | for (var j = 0; j < grid[i].Length; j++) |
| 181 | 30 | | { |
| 181 | 31 | | if (grid[i][j] == 0) |
| 121 | 32 | | { |
| 121 | 33 | | continue; |
| | 34 | | } |
| | 35 | |
|
| 60 | 36 | | maxGold = Math.Max(maxGold, GetMaximumGold(grid, i, j, 0)); |
| 60 | 37 | | } |
| 30 | 38 | | } |
| | 39 | |
|
| 7 | 40 | | return maxGold; |
| 7 | 41 | | } |
| | 42 | |
|
| | 43 | | private static int GetMaximumGold(IReadOnlyList<int[]> grid, int i, int j, int currentGold) |
| 1020 | 44 | | { |
| 1020 | 45 | | var cell = grid[i][j]; |
| | 46 | |
|
| 1020 | 47 | | currentGold += cell; |
| | 48 | |
|
| 1020 | 49 | | grid[i][j] = 0; |
| | 50 | |
|
| 1020 | 51 | | var maxGold = currentGold; |
| | 52 | |
|
| 1020 | 53 | | if (i + 1 < grid.Count && grid[i + 1][j] != 0) |
| 290 | 54 | | { |
| 290 | 55 | | maxGold = Math.Max(maxGold, GetMaximumGold(grid, i + 1, j, currentGold)); |
| 290 | 56 | | } |
| | 57 | |
|
| 1020 | 58 | | if (i - 1 >= 0 && grid[i - 1][j] != 0) |
| 336 | 59 | | { |
| 336 | 60 | | maxGold = Math.Max(maxGold, GetMaximumGold(grid, i - 1, j, currentGold)); |
| 336 | 61 | | } |
| | 62 | |
|
| 1020 | 63 | | if (j + 1 < grid[i].Length && grid[i][j + 1] != 0) |
| 211 | 64 | | { |
| 211 | 65 | | maxGold = Math.Max(maxGold, GetMaximumGold(grid, i, j + 1, currentGold)); |
| 211 | 66 | | } |
| | 67 | |
|
| 1020 | 68 | | if (j - 1 >= 0 && grid[i][j - 1] != 0) |
| 123 | 69 | | { |
| 123 | 70 | | maxGold = Math.Max(maxGold, GetMaximumGold(grid, i, j - 1, currentGold)); |
| 123 | 71 | | } |
| | 72 | |
|
| 1020 | 73 | | grid[i][j] = cell; |
| | 74 | |
|
| 1020 | 75 | | return maxGold; |
| 1020 | 76 | | } |
| | 77 | | } |