Skip to content

Instantly share code, notes, and snippets.

@BeautyfullCastle
Created October 11, 2019 09:02
Show Gist options
  • Save BeautyfullCastle/9dbd08972bcc7d0bbba2685295450575 to your computer and use it in GitHub Desktop.
Save BeautyfullCastle/9dbd08972bcc7d0bbba2685295450575 to your computer and use it in GitHub Desktop.
using System;
namespace CSharpPractice
{
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[2, 4]
{
{ 13, -2, 3, 2 },
{ -1, 2, 4, 7 }
};
int[,] mem = new int[2, 4];
Console.WriteLine(GetBiggestSum(ref arr, ref mem, 1, 3));
}
private static int GetBiggestSum(ref int[,] arr, ref int[,] mem, int i, int j)
{
if (mem[i, j] > 0)
{
return mem[i, j];
}
if (i == 0 & j == 0)
{
return mem[i, j] = arr[i, j];
}
else if (i == 0)
{
return mem[i, j] = arr[i, j] + GetBiggestSum(ref arr, ref mem, i, j - 1);
}
else if (j == 0)
{
return mem[i, j] = arr[i, j] + GetBiggestSum(ref arr, ref mem, i - 1, j);
}
return mem[i, j] = arr[i, j]
+ Math.Max(GetBiggestSum(ref arr, ref mem, i - 1, j - 1),
Math.Max(GetBiggestSum(ref arr, ref mem, i - 1, j), GetBiggestSum(ref arr, ref mem, i, j - 1)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment