This problem, also referred to as the “sliding window maximum” problem, can be efficiently solved using heaps. We’ve already discussed an O(n) solution.
This method has the limitation that it will work for only those matrix where the first integer of each row is greater than the last integer of the previous row.
Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array.