1

この質問は次のようになります。

しかし、重複する日付範囲の最大数を取得するにはどうすればよいですか? (できれば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同時日付範囲の最大数はどこになりますか。

4

1 に答える 1

4
  • 範囲ごとに、Tuple<DateTime, int>start, +1を持つ 2 つの を作成し、end, -1
  • タプルのコレクションを日付順に並べ替える
  • ソートされたリストを繰り返し、タプルの数値部分を現在の合計に追加し、現在の合計が到達した最大値を追跡します
  • 現在の合計が到達する最大値を返します

O(n log n)ソートのために実行されます。おそらくもっと効率的な方法があります。

于 2012-08-14T16:25:10.170 に答える