492. Matrix Median

MediumBinary SearchBinary SearchMatrix

Given a matrix where each row is sorted in ascending order, return the median of all its elements. When the total number of elements is even, return the lower median (the element at 0-based index (total-1)/2 of the fully sorted values). The matrix is given as a JSON array of rows.

Input: A JSON array of sorted rows.

Output: Integer — the median value.

Examples

Example 1
Input: [[1,3,5],[2,6,9],[3,6,9]]
Output: 5
Explanation: Sorted values: median is 5.
Example 2
Input: [[1,3,4]]
Output: 3
Explanation: Single row median.
Example 3
Input: [[5]]
Output: 5
Explanation: Single element.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →