Skip to content

Instantly share code, notes, and snippets.

@Ezeji
Created November 13, 2021 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ezeji/35fc747c4c02299bf4c5b904616ade0d to your computer and use it in GitHub Desktop.
Save Ezeji/35fc747c4c02299bf4c5b904616ade0d to your computer and use it in GitHub Desktop.
The monsoon umbrellas algorithm finds the minimum number of umbrellas that can cover a given number of people and no more.
using System;
using System.Collections.Generic;
namespace Monsoon_Umbrellas_Algorithm
{
class Program
{
public static int GetMinimumUmbrellas(int[] sizes, int requirement)
{
int result = 0;
int arrayAddition = 0;
int i = 0;
int mid = sizes.Length / 2;
Array.Sort(sizes);
if (sizes.Length > 2)
{
while (mid > i)
{
arrayAddition += sizes[mid];
if (sizes[mid] == requirement)
{
result = 1;
}
else if (arrayAddition == requirement)
{
result = i + 1;
}
else
{
result = -1;
}
mid--;
i++;
}
}
else
{
for (int j = 0; j < sizes.Length; j++)
{
arrayAddition += sizes[j];
if (sizes[j] == requirement)
{
result = 1;
}
else if (arrayAddition == requirement)
{
result = j + 1;
}
else
{
result = -1;
}
}
}
return result;
}
static void Main(string[] args)
{
//int[] sizes = new int[2] { 3, 5 };
//int requirement = 5;
int[] sizes = new int[7] { 1, 2, 3, 4, 5, 6, 9 };
int requirement = 7;
int result = GetMinimumUmbrellas(sizes, requirement);
Console.WriteLine(result.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment