Uber interview question

Medium LC question (2D matrix)