feat : Find the Maximum Number of Fruits Collected
This commit is contained in:
10
202508/3363 MaxCollectedFruits/C#/MaxCollectedFruits.csproj
Normal file
10
202508/3363 MaxCollectedFruits/C#/MaxCollectedFruits.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
24
202508/3363 MaxCollectedFruits/C#/MaxCollectedFruits.sln
Normal file
24
202508/3363 MaxCollectedFruits/C#/MaxCollectedFruits.sln
Normal file
@@ -0,0 +1,24 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.5.2.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaxCollectedFruits", "MaxCollectedFruits.csproj", "{AA67CF61-67B3-7229-E3F9-D471B0EF4EDE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{AA67CF61-67B3-7229-E3F9-D471B0EF4EDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{AA67CF61-67B3-7229-E3F9-D471B0EF4EDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{AA67CF61-67B3-7229-E3F9-D471B0EF4EDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{AA67CF61-67B3-7229-E3F9-D471B0EF4EDE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {B9D47F51-61C3-4A24-890A-01C9AC85EC93}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
67
202508/3363 MaxCollectedFruits/C#/Program.cs
Normal file
67
202508/3363 MaxCollectedFruits/C#/Program.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
public class Solution
|
||||
{
|
||||
public int MaxCollectedFruits(int[][] fruits)
|
||||
{
|
||||
int n = fruits.Length;
|
||||
int ans = 0;
|
||||
for (int i = 0; i < n; ++i) ans += fruits[i][i];
|
||||
|
||||
int dp()
|
||||
{
|
||||
int[] prev = Enumerable.Repeat(int.MinValue, n).ToArray();
|
||||
int[] curr = new int[n];
|
||||
prev[n - 1] = fruits[0][n - 1];
|
||||
for (int i = 1; i < n - 1; ++i)
|
||||
{
|
||||
Array.Fill(curr, int.MinValue);
|
||||
for (int j = Math.Max(n - 1 - i, i + 1); j < n; ++j)
|
||||
{
|
||||
int best = prev[j];
|
||||
if (j - 1 >= 0)
|
||||
{
|
||||
best = Math.Max(best, prev[j - 1]);
|
||||
}
|
||||
if (j + 1 < n)
|
||||
{
|
||||
best = Math.Max(best, prev[j + 1]);
|
||||
}
|
||||
curr[j] = best + fruits[i][j];
|
||||
}
|
||||
var temp = prev;
|
||||
prev = curr;
|
||||
curr = temp;
|
||||
}
|
||||
return prev[n - 1];
|
||||
}
|
||||
|
||||
ans += dp();
|
||||
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int j = 0; j < i; ++j)
|
||||
{
|
||||
var temp = fruits[j][i];
|
||||
fruits[j][i] = fruits[i][j];
|
||||
fruits[i][j] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
ans += dp();
|
||||
return ans;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
var Solution = new Solution();
|
||||
|
||||
var fruits = new int[][] { [1,2,3,4],[5,6,8,7],[9,10,11,12],[13,14,15,16]};
|
||||
|
||||
Console.WriteLine(Solution.MaxCollectedFruits(fruits));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user