Sum of Largest Numbers in Each Row
Given a 2D matrix consisting of integers, find the sum of the largest number from each row and return the total sum. Each row in the matrix may contain any number of elements, and the matrix can contain any number of rows.
[ [ 1, 2, 3 ], [ 4, 5, 0 ], [ -1, -2, -3 ] ]
Explanation. The largest numbers in each row are 3, 5, and -1. Their sum is 3 + 5 - 1 = 7.
[ [ -10, -20, -30, -40 ], [ 55 ], [ 15, 25 ] ]
Explanation. The largest numbers in each row are -10, 55, and 25. Their sum is -10 + 55 + 25 = 70.
[ [ 1, 2 ], [ 3 ], [ 4, 5, 6 ] ]
Explanation. The largest numbers in each row are 2, 3, and 6. Their sum is 2 + 3 + 6 = 11.
[ [ 100 ] ]
Explanation. There is only one row and the largest number is 100.
Follow-up: How would your solution adapt if asked to find the sum of the smallest numbers in each row instead?
1. The matrix will not be empty and will have at least one row. 2. Each row will contain at least one integer. 3. The integers in the matrix can be both positive and negative.
- Views
- 3