Created
June 3, 2014 03:22
-
-
Save briandiaz/a8ee1f990e3ce63a787f to your computer and use it in GitHub Desktop.
Get the max number of folders to choose in a way that minimizes the quantity of folders that a person have to check.
This file contains hidden or 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
/* | |
* Se tienen 3 personas y los siguientes archiveros: | |
* k = 3 personas | |
* Archiveros: | |
* 10 3 7 8 15 9 4 5 | |
* Cuál es el número de folder máximo a escoger de forma tal que se minimice la cantidad | |
* de folders que cada persona debe chequear? | |
* | |
* Se puede tener: | |
* 13 30 18: Esto sale de 10+3,7+8+15,9+4+5 | |
* 20 32 9: Esto sale de 10+3+7, 8+15+9, 4+5 | |
* 28 24 9: Esto sale de 10+3+7+8, 15+9, 4+5 | |
* 20 23 18: Esto sale de 10+3+7, 8+15, 9+4+5 | |
* Esta última es la mejor solución(20-23-18), ya que la máxima cantidad de gabinetes que una persona | |
* chequearía es 23, y esa es la cantidad minimizada que una persona puede chequear, | |
* para ser justos en cuanto a los folders que distintas personas deben chequear. | |
* | |
* | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace MaximunMinimunQuantityOfFolders | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int[] _directories = { 20, 55, 14, 17, 30, 69, 13, 10, 20, 55 }; | |
Console.WriteLine("Result: {0}", MaxMinFolders(_directories, 4)); | |
Console.ReadKey(); | |
} | |
public static int MaxMinFolders(int[] _array, int _persons) | |
{ | |
int Left = Max(_array); | |
int Right = Sum(_array); | |
int Mid; | |
int TSum; | |
int Person; | |
while (Left < Right) | |
{ | |
Mid = (Left + Right) / 2; | |
TSum = 0; | |
Person = 1; | |
for (int i = 0; i < _array.Length; i++) | |
{ | |
if ((TSum + _array[i]) <= Mid) | |
{ | |
TSum += _array[i]; | |
} | |
else | |
{ | |
Person++; | |
TSum = _array[i]; | |
} | |
} | |
if (Person > _persons) | |
Left = Mid + 1; | |
else | |
Right = Mid; | |
} | |
return Left; | |
} | |
private static int Max(int[] _array) | |
{ | |
return _array.Max(); | |
} | |
private static int Sum(int[] _array) | |
{ | |
return _array.Sum(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment