using System.Collections.Generic;
public static void Main()
new List<decimal> { 5.3M, 5.1M, 4.2M, 4.1M, 1.1M, },
new List<decimal> { 5.3M, 5.1M, 3.2M, 4.1M, 1.1M, },
new List<decimal> { 1.0M, 5.3M, 4.1M, 3.2M, 4.3M, 1.1M, },
new List<decimal> { 5.3M, 4.1M, 3.2M, 4.3M, 1.1M, },
new List<decimal> { 5.3M, 4.1M, 3.2M, 3.5M, },
new List<decimal> { 5M, 2M, 4M, 3M, 1M, 2M, 1M},
Console.WriteLine("input: " + string.Join(", ",s));
var r = RemoveIllegal(s);
Console.WriteLine("output: " + string.Join(", ",r));
Console.WriteLine("-------------------------------------");
private static List<decimal> RemoveIllegal(List<decimal> source)
var candidates = new Stack<List<decimal>>();
var result = default(List<decimal>);
while (candidates.TryPop(out var currentCandidate))
var prev = currentCandidate[0];
while (index < currentCandidate.Count)
var curr = currentCandidate[index];
var newCandidate = new List<decimal>(currentCandidate);
newCandidate.RemoveAt(index);
candidates.Push(newCandidate);
while (index > 0 && currentCandidate[index-1]< currentCandidate[index])
currentCandidate.RemoveAt(index - 1);
result = currentCandidate;
result = currentCandidate.Count > result.Count ? currentCandidate : result;