C# 计算出连续座位的最大数量
我让一位面试官让我用c#写一个程序,计算出一个场地内最多可以连续坐4人的家庭,考虑到4人必须连续坐在一排,上下文如下:C# 计算出连续座位的最大数量,c#,algorithm,linq,logic,C#,Algorithm,Linq,Logic,我让一位面试官让我用c#写一个程序,计算出一个场地内最多可以连续坐4人的家庭,考虑到4人必须连续坐在一排,上下文如下: N表示可用的行数 这些列从字母“A”到“K”进行标记,目的是表示字母“i”(换句话说,{A、B、C、D、E、F、G、H、J、K}) M表示保留座位的列表 快速示例: N = 2 M = {"1A","2F","1C"} Solution = 3 在图示中,您可以看到,在保留座位和给定大小的情况下,只有三个四口之家可以连续就座 你将如何解决这个问题?是否可以不使用for循
- N表示可用的行数
- 这些列从字母“A”到“K”进行标记,目的是表示字母“i”(换句话说,{A、B、C、D、E、F、G、H、J、K})
- M表示保留座位的列表
N = 2
M = {"1A","2F","1C"}
Solution = 3
在图示中,您可以看到,在保留座位和给定大小的情况下,只有三个四口之家可以连续就座
你将如何解决这个问题?是否可以不使用for循环?(Linq解决方案)
当我试图处理保留时,我陷入了for循环:我的想法是获得一行的所有保留,但我真的不知道如何处理字母(直接从字母转换为数字是不可能的,因为缺少了“I”)不管怎样,你都需要这些字母来定位预留座位
任何关于如何解决这个问题的方法或见解都会很好。
提前谢谢 如果您用简单的开发人员格式表示矩阵,将更容易。您可以通过字典或手动执行不太复杂的映射来完成它。在任何情况下,这将计算免费连续座位数:
public static void Main(string[] args)
{
var count = 0;//total count
var N = 2; //rows
var M = 10; //columns
var familySize = 4;
var matrix = new []{Tuple.Create(0,0),Tuple.Create(1,5), Tuple.Create(0,2)}.OrderBy(x=> x.Item1).ThenBy(x=> x.Item2).GroupBy(x=> x.Item1, x=> x.Item2);
foreach(var row in matrix)
{
var prevColumn = -1;
var currColumn = 0;
var free = 0;
var div = 0;
//Instead of enumerating entire matrix, we just calculate intervals in between reserved seats.
//Then we divide them by family size to know how many families can be contained within
foreach(var column in row)
{
currColumn = column;
free = (currColumn - prevColumn - 1)/familySize;
count += free;
prevColumn = currColumn;
}
currColumn = M;
free = (currColumn - prevColumn - 1)/familySize;
count += free;
}
Console.WriteLine("Result: {0}", count);
}
祝你面试顺利
像往常一样,你会被问到如何改进这一点?所以你会考虑像O(n),O(WTF)之类的复杂事物。
底层实现总是需要for
或foreach
。重要的是,不要在循环中做不必要的事情。例如,如果一排中只剩下3个座位,您不需要在那一排继续搜索,因为不可能找到任何座位
这可能有点帮助:
var n = 2;
var m = new string[] { "1A", "2F", "1C" };
// We use 2 dimension bool array here. If it is memory constraint, we can use BitArray.
var seats = new bool[n, 10];
// If you just need the count, you don't need a list. This is for returning more information.
var results = new List<object>();
// Set reservations.
foreach (var r in m)
{
var row = r[0] - '1';
// If it's after 'H', then calculate index based on 'J'.
// 8 is index of J.
var col = r[1] > 'H' ? (8 + r[1] - 'J') : r[1] - 'A';
seats[row, col] = true;
}
// Now you should all reserved seats marked as true.
// This is O(N*M) where N is number of rows, M is number of columns.
for (int row = 0; row < n; row++)
{
int start = -1;
int length = 0;
for (int col = 0; col < 10; col++)
{
if (start < 0)
{
if (!seats[row, col])
{
// If there's no consecutive seats has started, and current seat is available, let's start!
start = col;
length = 1;
}
}
else
{
// If have started, check if we could have 4 seats.
if (!seats[row, col])
{
length++;
if (length == 4)
{
results.Add(new { row, start });
start = -1;
length = 0;
}
}
else
{
// // We won't be able to reach 4 seats, so reset
start = -1;
length = 0;
}
}
if (start < 0 && col > 6)
{
// We are on column H now (only have 3 seats left), and we do not have a consecutive sequence started yet,
// we won't be able to make it, so break and continue next row.
break;
}
}
}
var solution = results.Count;
然后可以开始使用LINQ。这里是另一个实现 我还试图解释为什么做了某些事情 祝你好运
private static int GetNumberOfAvailablePlacesForAFamilyOfFour(int numberOfRows, string[] reservedSeats)
{
// By just declaring the column names as a string of the characters
// we can query the column index by colulmnNames.IndexOf(char)
string columnNames = "ABCDEFGHJK";
// Here we transform the reserved seats to a matrix
// 1A 2F 1C becomes
// reservedSeatMatrix[0] = [0, 2] -> meaning row 1 and columns A and C, indexes 0 and 2
// reservedSeatMatrix[1] = [5] -> meaning row 2 and column F, index 5
List<List<int>> reservedSeatMatrix = new List<List<int>>();
for (int row = 0; row < numberOfRows; row++)
{
reservedSeatMatrix.Add(new List<int>());
}
foreach (string reservedSeat in reservedSeats)
{
int seatRow = Convert.ToInt32(reservedSeat.Substring(0, reservedSeat.Length - 1));
int seatColumn = columnNames.IndexOf(reservedSeat[reservedSeat.Length - 1]);
reservedSeatMatrix[seatRow - 1].Add(seatColumn);
}
// Then comes the evaluation.
// Which is simple enough to read.
int numberOfAvailablePlacesForAFamilyOfFour = 0;
for (int row = 0; row < numberOfRows; row++)
{
// Reset the number of consecutive seats at the beginning of a new row
int numberOfConsecutiveEmptySeats = 0;
for (int column = 0; column < columnNames.Length; column++)
{
if (reservedSeatMatrix[row].Contains(column))
{
// reset when a reserved seat is reached
numberOfConsecutiveEmptySeats = 0;
continue;
}
numberOfConsecutiveEmptySeats++;
if(numberOfConsecutiveEmptySeats == 4)
{
numberOfAvailablePlacesForAFamilyOfFour++;
numberOfConsecutiveEmptySeats = 0;
}
}
}
return numberOfAvailablePlacesForAFamilyOfFour;
}
static void Main(string[] args)
{
int familyPlans = GetNumberOfAvailablePlacesForAFamilyOfFour(2, new string[] { "1A", "2F", "1C" });
}
private static int GetNumberOfAvailablePlacesForAFamilyOfFour(int numberOfRows,string[]reservedSeats)
{
//只需将列名声明为字符串
//我们可以通过colulmnNames.IndexOf(char)查询列索引
string columnNames=“ABCDEFGHJK”;
//这里,我们将保留座位转换为矩阵
//1a2f 1C变为
//reservedSeatMatrix[0]=[0,2]->表示第1行、A列和C列、索引0和2
//reservedSeatMatrix[1]=[5]->表示第2行和第F列的索引5
List reservedSeatMatrix=新列表();
for(int row=0;row
字母到索引的转换可以用字典来完成,或者如果你想用char[]
来完成,字母的索引就是行中的索引。我们希望采访者不会在查询具有bbbbbbbbbb
(8个空格)、bbbbbbx&&xbbbbb
(2乘以4个空格)的字符串和bbbb
(4个空格)。如果您使用查询,这些组合应该会给出结果
private static int GetNumberOfAvailablePlacesForAFamilyOfFour(int numberOfRows, string[] reservedSeats)
{
// By just declaring the column names as a string of the characters
// we can query the column index by colulmnNames.IndexOf(char)
string columnNames = "ABCDEFGHJK";
// Here we transform the reserved seats to a matrix
// 1A 2F 1C becomes
// reservedSeatMatrix[0] = [0, 2] -> meaning row 1 and columns A and C, indexes 0 and 2
// reservedSeatMatrix[1] = [5] -> meaning row 2 and column F, index 5
List<List<int>> reservedSeatMatrix = new List<List<int>>();
for (int row = 0; row < numberOfRows; row++)
{
reservedSeatMatrix.Add(new List<int>());
}
foreach (string reservedSeat in reservedSeats)
{
int seatRow = Convert.ToInt32(reservedSeat.Substring(0, reservedSeat.Length - 1));
int seatColumn = columnNames.IndexOf(reservedSeat[reservedSeat.Length - 1]);
reservedSeatMatrix[seatRow - 1].Add(seatColumn);
}
// Then comes the evaluation.
// Which is simple enough to read.
int numberOfAvailablePlacesForAFamilyOfFour = 0;
for (int row = 0; row < numberOfRows; row++)
{
// Reset the number of consecutive seats at the beginning of a new row
int numberOfConsecutiveEmptySeats = 0;
for (int column = 0; column < columnNames.Length; column++)
{
if (reservedSeatMatrix[row].Contains(column))
{
// reset when a reserved seat is reached
numberOfConsecutiveEmptySeats = 0;
continue;
}
numberOfConsecutiveEmptySeats++;
if(numberOfConsecutiveEmptySeats == 4)
{
numberOfAvailablePlacesForAFamilyOfFour++;
numberOfConsecutiveEmptySeats = 0;
}
}
}
return numberOfAvailablePlacesForAFamilyOfFour;
}
static void Main(string[] args)
{
int familyPlans = GetNumberOfAvailablePlacesForAFamilyOfFour(2, new string[] { "1A", "2F", "1C" });
}