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;
}
Source: stackoverflow.com
Related Query
- How to get max sum range for specific time period from values in Dictionary
- Get keys as List<> from Dictionary for certain values
- Sum values of a Dictionary for specific keys C#
- How to get gap in date ranges from a period of time
- Get values from database with specific time interval
- Get a list of specific values from a C# Dictionary
- How to check List for value in a specific range of values in .NET
- C# - dictionary - How to get max of specific class value in dictionary?
- Get objects from dictionary with equal properties values for every Key
- How to get flattened list from dictionary key query where values are lists?
- how to get max time from a report
- Get data in a specific date for a time range with a minute difference
- How to get a specific dictionary key value from ListDictionary?
- How to get Max and Min IN/OUT time for each date in LINQ?
- How to get the values from specific Xelement based on condition in linq
- How to select only specific time records for each day from C# DataTable using linq
- Entity Framework EF Query using LINQ for related Entities - Getting Customers with their orders from specific period of time only
- How to get the max count of rows for specific field?
- How to get values from IGrouping
- How can I get LINQ to return the object which has the max value for a given property?
- How to select values within a provided index range from a List using LINQ
- How to get MAX value from Dictionary?
- How to select multiple values from a Dictionary using Linq as simple as possible
- How to calculate the sum of all values in a dictionary excluding the first item's value?
- LINQ: Getting Keys for a given list of Values from Dictionary and vice versa
- VB.NET LINQ Query: Getting The Sum of All Values For A Specific Structure Member
- How do I sum values from two dictionaries in C#?
- How do I get the sum of the Counts of nested Lists in a Dictionary without using foreach?
- How to get only specific field from the list
- How to get item from dictionary by value of property
More Query from same tag
- Convert list to dictionary using linq and not worrying about duplicates
- Custom where can't run
- How to convert linq results to HashSet or HashedSet
- Linq Expression that compare Lambda result with a constant
- LINQ SqlMethods.Like (and .Contains) fails
- Orderby a string column in linq causes an error
- How to change tag name and get attribute using linq-to-xml
- How to implement SQL where in for entity framework 6
- Access to the return value from the database by linq
- How can I iterate over several IEnumerables simultaneously
- Need Left Outer Join in LINQ using Extension Method /Query Syntax
- Select Data based on related data exist
- Smalldatetime ToString in LINQ
- Linq Union/UnionAll/Concat
- Get a list of objects from DB, create a ViewModel and populate its property by single LINQ statement
- Requirements for collection class to be used with LINQ
- Looking for example or tips on how to iterate over a table based on results
- How to create AutoMap feature for FluentNHibernate based on attributes?
- Rollback Inserts/Updates/Deletes in linq pad?
- A specified Include path is not valid. The EntityType 'SpiceShop.Models.Product' does not declare a navigation property with the name 'Products'
- LINQ: Aggregate Rows based on dictionary or mapping?
- Find next incremental value not in existing list using linq
- How can I move next/previous in List using LINQ?
- Entity Framework Linq Query: How to Where on Multiple Nav Properties and Select from 3rd Nav Property
- COUNT(1) OVER () with Linq to Sql
- Where command, XML Feed Windows phone 8
- need to do a subquery in Linq
- Using NOT operator with AddressOf in a LINQ Where
- Linq Query with SUM and ORDER BY
- How to optimize and pre-compile this LINQ expression?