编码挑战:连续数字的最大可能总和 [英] Coding challenge: largest possible sum from consecutive numbers
问题描述
给定一个整数数组 - 一些是正数,一些是负数,一些是否为正数,在这个数组中找到具有最大可能总和的连续数字集。
显然,如果所有数字都是正数,答案就是初始数组。如果所有都是否定的则答案是空数组。中间的情况是有趣的。
Given an array of integers - some positive, some negative, some neither, find the set of consecutive numbers in this array with the largest possible sum.
Obviously if all the numbers are positive the answer is the initial array. If all are negative then the answer is an empty array. The in-between case is the interesting one.
推荐答案
更新3:添加了VB.Net版本
这是另一个快速的:
Update 3: Added VB.Net versions
Here is another quick one:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LargestGroupSum
{
static class Program
{
static void Main(string[] args)
{
foreach (var test in new List<List<int>>()
{
new List<int>(),
new List<int>() { -1, -2, -5, -4, -5, -1, -2, -11 },
new List<int>() { 1, 2, 5, 4, 5, 1, 2, 11 },
new List<int>() { 1, 2, -5, 4, 5, -1, 2, -11 },
new List<int>() { 1, 2, 4, -20, 4, 2, 1, -15, 3, 2, 2 },
new List<int>() { 5, -5, 5, -10, 5, -5, 5 },
new List<int>() { 5, -5, 5, -5, 5 },
new List<int>() { 5, 5, -5, -6 },
new List<int>() { -1, -1, 1, -1, 2 }
})
{
var lg = test.LargestGroupSumList();
Console.WriteLine(
For [{string.Join (, ,test)}],最大的组是[{string.Join(, ,lg)}] with {(lg.Any()?
"For [{string.Join(",", test)}], the largest group is [{string.Join(",", lg)}] with {(lg.Any() ?
总和{lg.Sum()} : no value )});
}
Console.WriteLine( \\ nn--按任意键退出 - );
Console.ReadKey();
}
}
public static class HelperExtensions
{
public static IList< int> LargestGroupSumList( this IList< int> list)
{
if (!list .Any(x = > x > = 1 ))
return new List< int>();
else if (!list.Any(x = > x < 1 ))
返回列表;
else
{
var groups = new List< List< int>>();
for ( int i = 0 ; i < list.Count; i ++)
for ( int j = 0 ; j < list.Count - i; j ++)
groups.Add(list.Skip(i).Take(j + 1 )。ToList());
return groups.OrderByDescending(X = > X.Count).OrderByDescending(x = > x.Sum())。First();
}
}
}
}
"a sum of {lg.Sum()}" : "no value")}"); } Console.WriteLine("\r\n-- Press any key to exit --"); Console.ReadKey(); } } public static class HelperExtensions { public static IList<int> LargestGroupSumList(this IList<int> list) { if (!list.Any(x => x >= 1)) return new List<int>(); else if (!list.Any(x => x < 1)) return list; else { var groups = new List<List<int>>(); for (int i = 0; i < list.Count; i++) for (int j = 0; j < list.Count - i; j++) groups.Add(list.Skip(i).Take(j + 1).ToList()); return groups.OrderByDescending(X => X.Count).OrderByDescending(x => x.Sum()).First(); } } } }
Imports System.Runtime.CompilerServices
Module Module1
Sub Main()
For Each test In New List(Of List(Of Integer))() From {
New List(Of Integer)(),
New List(Of Integer)() From {-1, -2, -5, -4, -5, -1, -2, -11},
New List(Of Integer)() From {1, 2, 5, 4, 5, 1, 2, 11},
New List(Of Integer)() From {1, 2, -5, 4, 5, -1, 2, -11},
New List(Of Integer)() From {1, 2, 4, -20, 4, 2, 1, -15, 3, 2, 2},
New List(Of Integer)() From {5, -5, 5, -10, 5, -5, 5},
New List(Of Integer)() From {5, -5, 5, -5, 5},
New List(Of Integer)() From {5, 5, -5, -6},
New List(Of Integer)() From {-1, -1, 1, -1, 2}}
Dim lg = test.LargestGroupSumList()
Console.WriteLine("For [{0}], the largest group is [{1}] with {2}", String.Join(",", test), String.Join(",", lg), If(lg.Any(), String.Format("a sum of {0}", lg.Sum()), "no value"))
Next
Console.WriteLine("{0}-- Press any key to exit --", vbCrLf)
Console.ReadKey()
End Sub
End Module
Public Module HelperExtensions
<Extension>
Public Function LargestGroupSumList(list As IList(Of Integer)) As IList(Of Integer)
If Not list.Any(Function(x) x >= 1) Then
Return New List(Of Integer)()
ElseIf Not list.Any(Function(x) x < 1) Then
Return list
Else
Dim groups = New List(Of List(Of Integer))()
For i As Integer = 0 To list.Count - 1
For j As Integer = 0 To list.Count - i - 1
groups.Add(list.Skip(i).Take(j + 1).ToList())
Next
Next
Return groups.OrderByDescending(Function(x) x.Count).OrderByDescending(Function(x) x.Sum()).First()
End If
End Function
End Module
输出:
Outputs:
For [], the largest group is [] with no value
For [-1,-2,-5,-4,-5,-1,-2,-11], the largest group is [] with no value
For [1,2,5,4,5,1,2,11], the largest group is [1,2,5,4,5,1,2,11] with a sum of 31
For [1,2,-5,4,5,-1,2,-11], the largest group is [4,5,-1,2] with a sum of 10
For [1,2,4,-20,4,2,1,-15,3,2,2], the largest group is [1,2,4] with a sum of 7
For [5,-5,5,-10,5,-5,5], the largest group is [5,-5,5] with a sum of 5
For [5,-5,5,-5,5], the largest group is [5,-5,5,-5,5] with a sum of 5
For [5,5,-5,-6], the largest group is [5,5] with a sum of 10
For [-1,-1,1,-1,2], the largest group is [1,-1,2] with a sum of 2
-- Press any key to exit --
你可以运行它在线 [ ^ ]
更新
这是另一个处理多个返回集的版本,它们是连续数字的最大可能总和。
And you can run it online[^]
Update
Here is another version that handles multiple return sets that are equally the largest possible sum of consecutive numbers.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LargestGroupSum
{
static class Program
{
static void Main(string[] args)
{
Console.WriteLine("Largest possible sum of consecutive numbers");
Console.WriteLine("===========================================");
foreach (var test in new List<List<int>>()
{
new List<int>(),
new List<int>() { -1, -2, -5, -4, -5, -1, -2, -11 },
new List<int>() { 1, 2, 5, 4, 5, 1, 2, 11 },
new List<int>() { 1, 2, -5, 4, 5, -1, 2, -11 },
new List<int>() { 1, 2, 4, -20, 4, 2, 1, -15, 3, 2, 2 },
new List<int>() { 5, -5, 5, -10, 5, -5, 5 },
new List<int>() { 5, -5, 5, -5, 5 },
new List<int>() { 5, 5, -5, -6 },
new List<int>() { -1, -1, 1, -1, 2 }
})
{
var results = test.LargestGroupSumList();
Console.WriteLine(
这篇关于编码挑战:连续数字的最大可能总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!