-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathContiguousSequenceLargestSum.cs
70 lines (57 loc) · 2.32 KB
/
ContiguousSequenceLargestSum.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
namespace PracticeQuestionsSharp.Exercises.Numbers
{
//Given an array of integers, find the contiguous sequence with the largest sum.
public static class ContiguousSequenceLargestSum
{
public static int LargestContiguousSum(this IList<int> collection)
{
MaxContiguousSum max = new MaxContiguousSum();
//Iterate through each entry in collection as a starting point
for (int i = 0; i < collection.Count; ++i)
{
FindLargestSequence(i, 0, collection, max);
}
//Print the sequence and then return the largest sum
max.MaxSequence.ForEach(i => Console.Write($"{i},"));
Console.Write('\n');
return max.MaxSum;
}
private static void FindLargestSequence(int startIndex, int count, IList<int> collection, MaxContiguousSum max)
{
//If the previous numbers sum to greater than zero, the path using those numbers must be greater than this path
if (PrevSumGreaterThanZero(startIndex, collection)) return;
int endIndex = startIndex + count;
if (endIndex >= collection.Count) return;
int sum = 0;
var sequence = new List<int>(count + 1);
//Get the sum of this sequence and compare it to the current max
for (int i = startIndex; i <= endIndex; ++i)
{
sum += collection[i];
sequence.Add(collection[i]);
}
if (sum > max.MaxSum)
{
max.MaxSum = sum;
max.MaxSequence = sequence;
}
//Recurse with the current starting point and sequence length + 1
FindLargestSequence(startIndex, count + 1, collection, max);
}
private static bool PrevSumGreaterThanZero(int startIndex, IList<int> collection)
{
int sum = 0;
for (int i = 0; i < startIndex; ++i)
sum += collection[i];
return sum > 0;
}
//Maintain access to max throughout recursed method
private class MaxContiguousSum
{
public int MaxSum { get; set; } = Int32.MinValue;
public List<int> MaxSequence { get; set; }
}
}
}