
You are given a matrix 'M' with 'R' rows and 'C' columns of integers.
Return an array containing the maximum value of each row in 'M'.
Let 'R' = 3, 'C' = 4, and 'M' be:
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]]
The maximum value of the first row is 4.
The maximum value of the second row is 8.
The maximum value of the third row is 12.
Therefore, the answer is [4 8 12].
The first line contains two integers, 'R' and 'C', representing the number of rows and columns in the matrix 'M'.
The next 'R' lines each contain 'C' integers, representing the elements of the matrix 'M'.
Output Format :
Return an array containing the maximum value of each row in 'M'.
Note :
You don’t need to print anything. Just implement the given function.
1 <= 'R, C' <= 10^3
-10^5 <= 'M[i][j]' <= 10^5
Time Limit: 1 sec
3 3
1 2 3
4 5 2
7 8 9
3 5 9
The input matrix 'M' has 3 rows and 3 columns:
[[1, 2, 3],
[4, 5, 2],
[7, 8, 9]]
The maximum value in the first row [1, 2, 3] is 3.
The maximum value in the second row [4, 5, 2] is 5.
The maximum value in the third row [7, 8, 9] is 9.
Thus, the answer is [3 5 9].
2 4
10 20 5 15
-5 -10 -2 -8
20 -2
Iterate through each row of the matrix and find the maximum element in that row.
Approach:
Algorithm:
O(R * C), where 'R' is the number of rows and 'C' is the number of columns in the matrix 'M'.
We iterate through each of the 'R' rows and for each row, we iterate through its 'C' columns to find the maximum. Thus, the overall time complexity is of the order O(R * C).
O(R), where 'R' is the number of rows in the matrix 'M'.
We create an array 'result' of size 'R' to store the maximum of each row. Thus, the overall space complexity is of the order O(R).