I am dong a competitive programming problem I have passed all the tests. but my program times out. I need help optimizing my code so it can run faster.
Task:
Finish method:
BingoCaller.GetNumber()
Return all numbers in the range of 1 until 75 once and in random order
If there are no numbers left, return an empty string
The numbers are returned one by one in Bingo style:
"I27", "N40", "B5", "B12", "I28", "O69", "B1", ...
These are the ranges that you must follow:
A number within range 1 to 15 starts with a 'B'
A number within range 16 to 30 starts with an 'I'
A number within range 31 to 45 starts with an 'N'
A number within range 46 to 60 starts with a 'G'
A number within range 61 to 75 starts with an 'O'
Use
System.Random
to generate your random numbers. Pass via the constructor for testing purposes.
My code below
using System;
using System.Collections.Generic;
public class BingoCaller
{
private Random random;
static List<int> List = new List<int>();
public BingoCaller(Random random)
{
this.random = random;
}
public string GetNumber()
{
Start:
var item = random.Next(75);
if (List.Contains(item))
{
goto Start;
}
else
{
List.Add(item);
if (item >= 61)
{
return "O" + item;
}
else if (item >= 46)
{
return "G" + item;
}
else if (item >= 31)
{
return "N" + item;
}
else if (item >= 16)
{
return "I" + item;
}
else
{
return "B" + item;
}
}
}
}