score:1

    public struct timevalue
    {
        public datetime time {get;set;}
        public int value{get;set;}
    }

    static void main(string[] args)
    {
        var txt = @"12/31/2013 10:00:01  96
        12/31/2013 10:00:02  20
        12/31/2013 10:00:03  51
        12/31/2013 10:00:04  62
        12/31/2013 10:00:05  84
        12/31/2013 10:00:06  78
        12/31/2013 10:00:07  74
        12/31/2013 10:00:08 150
        12/31/2013 10:00:09 130
        12/31/2013 10:00:10  99
        12/31/2013 10:00:11 101
        12/31/2013 10:00:12 123
        12/31/2013 10:00:13  51
        12/31/2013 10:00:14  61
        12/31/2013 10:00:15  19
        12/31/2013 10:00:16  81
        12/31/2013 10:00:17  98
        12/31/2013 10:00:18  39
        12/31/2013 10:00:19  45
        12/31/2013 10:00:20  65";

        var values = 
            (from line in txt.split(new[]{environment.newline}, stringsplitoptions.removeemptyentries)
            let pair = line.split(new[]{' '}, 3, stringsplitoptions.removeemptyentries)
            select new timevalue
            {
                time = datetime.parseexact(pair[0] + " " + pair[1], "mm/dd/yyyy hh:mm:ss", cultureinfo.invariantculture),
                value = int.parse(pair[2])
            })
            .orderby(e => e.time)
            .toarray();;

        timespan range = timespan.fromseconds(5);

        int totalmax = -1;
        int maxtail = -1;
        int maxhead = -1;

        int tail = 0; // index at tail of the window
        int currentmax = 0;
        for (int head = 0; head < values.length; head++) // head of the window
        {
            currentmax += values[head].value; // add next value
            datetime tailtime = values[head].time - range;
            while (values[tail].time <= tailtime) // remove values at tail that don't fit in range
            {
                currentmax -= values[tail].value;
                tail++;
            }

            if (currentmax > totalmax)
            {
                totalmax = currentmax;
                maxtail = tail;
                maxhead = head;
            }
        }

        console.writeline("maximum range from times:" + values[maxtail].time + " - " + values[maxhead].time);
    }

score:1

void main()
{
    var txt = @"12/31/2013 10:00:01  96
    12/31/2013 10:00:02  20
    12/31/2013 10:00:03  51
    12/31/2013 10:00:04  62
    12/31/2013 10:00:05  84
    12/31/2013 10:00:06  78
    12/31/2013 10:00:07  74
    12/31/2013 10:00:08 150
    12/31/2013 10:00:09 130
    12/31/2013 10:00:10  99
    12/31/2013 10:00:11 101
    12/31/2013 10:00:12 123
    12/31/2013 10:00:13  51
    12/31/2013 10:00:14  61
    12/31/2013 10:00:15  19
    12/31/2013 10:00:16  81
    12/31/2013 10:00:17  98
    12/31/2013 10:00:18  39
    12/31/2013 10:00:19  45
    12/31/2013 10:00:20  65";
    var data = 
        (from line in txt.split(new[]{environment.newline}, stringsplitoptions.removeemptyentries)
        let pair = line.split(new[]{' '}, 3, stringsplitoptions.removeemptyentries)
        select new entry
        {
            dt = datetime.parse(pair[0] + " " + pair[1]),
            val = int.parse(pair[2])
        })
        .orderby(e => e.dt);

    var range = timespan.fromseconds(5);
    var queue = new queue<entry>();
    int max = 0;
    datetime? maxstart = null;
    datetime? maxend = null;
    int sum = 0;

    foreach (var entry in data)
    {
        queue.enqueue(entry);
        sum += entry.val;
        while(queue.count > 0 && entry.dt - queue.peek().dt >= range)
        {
            sum -= queue.dequeue().val;
        }
        if(sum > max)
        {
            max = sum;
            maxstart = queue.peek().dt;
            maxend = entry.dt;
        }
    }
    console.writeline("max is {0}, between {1} and {2}", max, maxstart, maxend);
}

public class entry
{
    public datetime dt;
    public int val;
}

Related Query

More Query from same tag