這個問題可能是類似於:如何確定重疊日期範圍的最大數量?
- Determine Whether Two Date Ranges Overlap
- Multiple Date range comparison for overlap: how to do it efficiently?
但是,我怎麼能得到重疊的日期範圍的最大值是多少? (最好是在C#)
實施例:(從 - 到)
01/01/2012 - 10/01/2012
03/01/2012 - 08/01/2012
09/01/2012 - 15/01/2012
11/01/2012 - 20/01/2012
12/01/2012 - 14/01/2012
結果= 3最大重疊的日期範圍
解:可能的實現通過@AakashM
提出的解決方案的List<Tuple<DateTime, int>> myTupleList = new List<Tuple<DateTime, int>>();
foreach (DataRow row in objDS.Tables[0].Rows) // objDS is a DataSet with the date ranges
{
var myTupleFrom = new Tuple<DateTime, int>(DateTime.Parse(row["start_time"].ToString()), 1);
var myTupleTo = new Tuple<DateTime, int>(DateTime.Parse(row["stop_time"].ToString()), -1);
myTupleList.Add(myTupleFrom);
myTupleList.Add(myTupleTo);
}
myTupleList.Sort();
int maxConcurrentCalls = 0;
int concurrentCalls = 0;
foreach (Tuple<DateTime,int> myTuple in myTupleList)
{
if (myTuple.Item2 == 1)
{
concurrentCalls++;
if (concurrentCalls > maxConcurrentCalls)
{
maxConcurrentCalls = concurrentCalls;
}
}
else // == -1
{
concurrentCalls--;
}
}
其中maxConcurrentCalls
將是最大併發日期範圍數。
您的意思是「總數」是「最大數量」還是可能是給定數量範圍的理論上可能的最大數量? – 2012-08-14 16:21:47
最大數量。還有其他2個重疊的範圍,但我只關心這個特定場景的最大數量 – aleafonso 2012-08-14 16:23:30
現在我明白你的意思了。您想知道在同一日期子範圍內重疊的範圍的最大數量。這裏在12/01和14/01之間,三個範圍(09/01-15/01),(11/01-20/01)和(12/01-14/01)確實重疊。 – 2012-08-14 16:37:23