Created
November 13, 2021 18:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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