eyal.safran
BAN USERSolution using state machine:
public enum PlotState
{
OnFlower,
OnEmpty,
AboutToPlant,
}
public static bool CanPlaceFlowers(List<bool> flowerbed, int numberToPlant)
{
int numberOfPlots = flowerbed.Count();
if (numberOfPlots < (numberToPlant * 2 - 1))
{
return false;
}
if (numberToPlant == 0)
{
return true;
}
int planted = 0;
PlotState state = PlotState.OnEmpty;
for (int i = 0; i < numberOfPlots; i++)
{
bool current = flowerbed[i];
switch (state)
{
case PlotState.AboutToPlant:
if (current)
{
state = PlotState.OnFlower;
}
else
{
state = PlotState.OnEmpty;
planted++;
if (planted == numberToPlant)
{
return true;
}
}
break;
case PlotState.OnEmpty:
if (current)
{
state = PlotState.OnFlower;
}
else
{
state = PlotState.AboutToPlant;
}
break;
case PlotState.OnFlower:
if (current)
{
// do nothing, state is the same
}
else
{
state = PlotState.OnEmpty;
}
break;
}
}
if (state == PlotState.AboutToPlant)
{
state = PlotState.OnFlower;
planted++;
if (planted == numberToPlant)
{
return true;
}
}
return false;
}
@hieu.pham overlapped meeting are adjacent to each other.
- eyal.safran July 10, 2015if you have 'k' meeting rooms, then you can assign each vertex 1 color out of these 'k'.
all the adjacent vertices must not have that same color, but among themselves they can have a different same color if they don't have a direct edge between them.
example:
M1: 13:00-14:00 - Color 1
M2: 12:30-13:30 - Color 2
M3: 13:30-14:00 - Color 2
(M2,M3) are adjacent to M1
M2 & M3 are not adjacent so the can share the same color