I have already made an algorithm for the problem but it still got a trouble.
Suppose that I have the recap of schedule (contains on gridview named GV):
TimeStart TimeEnd TotalOccuredOnThisTime
----------------------------------------------
08.00 08.50 1
08.00 09.40 43
08.00 10.50 2
Pls take a look to the picture below for more clear information:

What I want to get from the algorithm is, to count the time that occured on the same period, e.g. on the time of 08.00, it’s occured 46 event (see the yellow colored row).
This is my algorithm:
Dim ColumnLength As Integer = GV.Rows.Count
Dim TimeStart(ColumnLength - 1) As Integer
Dim TimeEnd(ColumnLength - 1) As Integer
Dim Total(ColumnLength - 1) As Integer
For i = 0 to ColumnLength - 1
TimeStart(i) = Convert.ToInt32(Regex.Replace(GV.Rows(i).Cells(0).Text, ".", ""))
TimeEnd(i) = Convert.ToInt32(Regex.Replace(GV.Rows(i).Cells(1).Text, ".", ""))
Total(i) = 0
Next
For i = 0 To ColumnLength - 2
For j = 0 To ColumnLength - 1
If TimeEnd(i) > TimeStart(j) And ((TimeStart(i) <= TimeEnd(j)) Or (TimeStart(i) >= TimeEnd(j)))
Total(j) = Total(j) - Total(i)
End If
Next
Next
But it’s resulted the wrong value.
The result what I want is like:
Time: 08.00 - 08.50 --> 46 event occured
08.50 - 09.40 --> 46 event occured
09.40 - 10.00 --> 45 event occured
etc...
How to do that properly? Really need your help…
You can use a
Dictionaryto keep track of the time ranges by concatenating the two time strings together for theKey(no need to cast to integers), and then for each occurrence of that range you could increment theValueportion of theDictionary.Additionally, there is no need to have nested loops using this approach, or a staging loop to populate start and end time arrays. The following algorithm should provide the results you are looking for:
Thus you could pull the count of the time range 09.40 – 10.00, for example, from the dictionary by saying:
I’ve verified this algorithm with the similar test code:
Update
After reading your clarification in your comment, you can still use the Dictionary to help keep track of this along with a couple helper classes (
TimeRange,TimeRangeCounter, andTimeRangeEqualityComparer). This does make things a bit more complicated though.The first helper class is used to hold the counts of exact and sub range matches:
The second helper class is used to help the dictionary know how one key (of type
TimeRange) differs from another:The Third helper class stores the start and end times of a range:
So using the above we should be able to write this algorithm: