So I just had a programming test for an interview and I consider myself a decent programmer, however I was unable to meet time constraints on the online test (and there was
The key part here is to work with the ranges and not with the array (until the end). What you can do is merge and split the ranges to keep a list of modifications. (the intermediate array is only used to visualize how the ranges overlap, you don't need to modify an array for each range added)
Start with an empty range (all zeros)
{0, 5, 0} -> [0, 0, 0, 0, 0]
add the first range
{0, 4, 0 } -> [0, 0, 0, 0, 0]
{0, 3, 143} -> [143, 143, 143]
[143, 143, 143, 0, 0] -> {0, 2, 143}, {3, 4, 0}
so you now have 2 ranges
{0, 3, 143}, {4, 5, 0} -> [143, 143, 143, 0, 0 ]
{2, 4, 100} -> [100, 100, 100]
[143, 143, 243, 100, 100] -> {0, 1, 143}, {2, 2, 243}, {4, 5, 100}
and now 3 and so on... When you get to the end you can just search the list of ranges for the largest.
The tricky part here of course is knowing how to merge, add, and split the ranges. If the beginning of an added range is within the range of another, then the first one must be split in two. Same as when the added range ends within the range of another. If the added range overlaps the whole range of another, then the range's value is simply added to it.