Thursday 15 July 2010

c# - Calculate maximum number of open events -


I have a list of open and close date events, such as:

  Date on Date close ----------- | ----------- 01.01.2000 | 05.01.2000 02.01.2000 | 02.01.2000   

then at 01.01. We have an open incident at 02.01. We have two open incidents and from there we have only one open incident till 05.01.

Now this task is to calculate the maximum number of open events , in this example it is 2.

I can not find a good solution for this, maybe someone else has a good idea. I have all the events in the linco-to-objects list, so ordering, filtering etc. is easy.

What have I done? Nothing, because I do not know where to start from :)

Here is a list solution to run I also included a division of open and closed pair (because I hope how your data is stored.) Since it is compulsory to walk open, so I joined earlier and not just sorted the date, and An order is required in the creation of the event object. If there is a closure before it is open, it will fail.

It was written and tested with linchpad. Copy and paste it as it is and run it.

I hope this is O (log n) because orderby should be logged in. Zero main () {List & lt; Event & gt; EList = new list & lt; Event & gt; (); EList.Add (new event (new date time (2000,1,1), new date time (2000,5,1)); EList.Add (new event (new date time (2000,2,1), new date time (2000,2,1)); Var datelist = eList.Select (item = & new; new {t = "open", d = item.open, s = item.open.Ticks * 10}) .Connect (eList.Select (item => new {T} = "shutdown", d = item.close, s = (item.close.Ticks * 10) +1}). Order By (item => item.s); var max = datelist.Aggregate ( New {curCount = 0, max = 0}, (results, items) => {if (item.t == "Open") {if (result.max> (result.curCount + 1)) Returns (New {curCount = result.curCount + 1, max = result.curCount + 1}) and return (new {curCount = result.curCount + 1, max = result.max});} return (new {curCount = result. Curcount-1, max = result.max});}, result => results.max); max.Dump ();} // Here define other methods and classes Public class event { Open Public DateTime {get; set;} Public Date Time Off {get; set;} Public Event (DateTime In-Open, Datetime Inclause) {If (Inon and Lt; = Inclause) {open = inOpen; Close = Close; } And throw (new exception ("can not be closed" + inClose.ToShortDateString () + "before opening" + opens. Shortstate string ())); }}

No comments:

Post a Comment