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.