score:5
Accepted answer
you could use this linq query:
int[] golfresult = { -3, +5, -3, 0, +1, +8, 0, +6, +2, -8, +5 };
var combinations = from i in enumerable.range(0, golfresult.length - 2)
select new {
i1 = golfresult[i],
i2 = golfresult[i + 1],
i3 = golfresult[i + 2],
};
var min = combinations.orderby(x => x.i1 + x.i2 + x.i3).first();
int[] mingolfresult = { min.i1, min.i2, min.i3 }; // -3, 0, +1
of course you need to check if there are at least three results in the array.
score:1
if you really want to do it in linq, you can go this way:
int length = 3;
var scores = new list<int>() { -3, +5, -3, 0, +1, +8, 0, +6, +2, -8, +5 };
var results =
scores
.select((value, index) => new
{
value = scores.skip(index - length + 1).take(length).sum(),
index = index - length + 1
})
.skip(length - 1)
.orderby(x => x.value)
.first()
.index;
this creates a second list that sums all length preceeding elements and then sorts it. you have
score:2
i'm not sure why you would do this with linq. i think a straight up iterative solution is easier to understand:
int[] scores = new[] { -3, 5, -3, 0, 1, 8, 0, 6, 2, -8, 5 };
int minimumsubsequence = int.maxvalue;
int minimumsubsequenceindex = -1;
for (int i = 0; i < scores.length - 2; i++)
{
int sum = scores[i] + scores[i + 1] + scores[i + 2];
if (sum < minimumsubsequence)
{
minimumsubsequence = sum;
minimumsubsequenceindex = i;
}
}
// minimumsubsequenceindex is index of the first item in the minimum subsequence
// minimumsubsequence is the minimum subsequence's sum.
Source: stackoverflow.com
Related Query
- How can I select the minimum sub-sequence using LINQ?
- How can I write the following code more elegantly using LINQ query syntax?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Using Linq to XML, how can I select the elements that have exactly x number of parent elements?
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- How can a LINQ join select only the first record?
- When using a LINQ Where clause on a Dictionary, how can I return a dictionary of the same type?
- How can I make my Linq select return values if the value selected is null?
- How can I code an outer join using LINQ and EF6?
- How can I use linq to select the columns of a jagged array
- In a LINQ select how can I select the FirstOrDefault of a list?
- How can you use LINQ to find Azure AD users with specific licenses using the Azure AD Graph API Client Library 2.0
- How can I select using LINQ for an entry that contains a LIST with more than one row?
- How can I get the top three players and their high scores from a collection using linq and lambdas
- How can we express the following code using query expression?
- How to write this code using the Linq Extension Method-Syntax?
- Using linq c# how can I select using a condition two different lists
- How can I select the most recent and distinct records using LINQ?
- LINQ - database query using "into", how do I reference data prior to "into" in the select clause?
- How do I select the top ten scores in a database table full of scores using linq in C#
- Using Linq lambdas, how can I get the first item in a two-key-sorted list?
- How to simplify the code Using LINQ
- How can I do a LINQ query joining two entities and select what a Where on one of the entities?
- Using Linq to object, how can I get one field's value based on another in the same list
- How can I move on column data to another column in the same row using linq
- How can I get a value inside the select query using linq?
- How can I edit my sequence so it returns one element only using LINQ
- How to write aggregate query in LINQ reusing part of the select code
- How can I return all the columns of a table using multiple distinct columns in Linq
- How can I select the most recent and distinct records and then count the different result types using LINQ?
More Query from same tag
- How to get a note node having particular hashtag value
- mscorlib error on LINQ
- HtmlAgilityPack select a specific number of nodes
- Add additional fields to Linq group by
- Get Specific object after a join using LINQ
- LINQ - Add to database does not show up
- combining two LINQ expressions into one
- Issues with LINQ statement with defining an element of a collection
- Storing results of multiple linq queries in IQueryable
- How to two joins in linq
- cannot find xml path error
- LINQ is Generating Extra IS NULL Condition in SQL Statement
- Why an error occurs after I modified my model class?
- How do I get a distinct list of list types
- Split list at marker elements with LINQ?
- How to use LINQ to perform a simultaneous search of all child items
- Linq select one of list where one of its child objects properties matches a value
- How to write the result of IGrouping?
- How to select greater number and lower number using LINQ To SQL in C#
- Enumerable.Where with a collection as parameter instead of extension method
- LINQ Select columns base on array of string[]
- How can I do simple order based validation on a collection of objects with LINQ?
- c#/linq data grouping
- ASP.NET MVC 3 Linq uppercase or lowercase database search
- 'The type or namespace name 'BookModel' could not be found ASP.NET MVC
- how to display all the data from database in MVC5 views
- Grouping linq to XML multiple hierarchies
- Code keeps eating Memory
- convert group by query to linq
- How to make a JOIN in LINQ to SQL equivalent to Not Equals