A seat-pricing service starts with every seat in a row at a surcharge of 0. Promotions and fees arrive as range updates: "add amount to every seat from start to end". After all updates, the team wants every seat's surcharge for small rows, and the highest surcharge for huge virtual rows.
Each operation is [start, end, amount]: add amount to every cell with index from start to end, inclusive. Cells are numbered 0 to n - 1. amount may be negative.
Write two functions:
applyRangeAdds(n, ops) returns the final array of all n values.
maxAfterRangeAdds(n, ops) returns { value, index }: the largest final value, and the smallest index that holds it. n can be as large as 1,000,000,000, so you cannot build the array.
Rules:
With no operations, every cell is 0.
If n is not a positive safe integer, throw a RangeError. If ops is not an array, or an operation is not three safe integers, throw a TypeError. If an operation breaks 0 <= start <= end < n, throw a RangeError.
There can be 200,000 operations, each covering up to the whole row, and |amount| <= 1,000,000,000. applyRangeAdds gets rows of up to 200,000 cells. Touching every cell of every range will not finish in time.