Maximum Row Sum
Easy matrix
Description
Constraints
1 <= rows, cols <= 1000
Examples
Input: matrix = [[1,2],[3,4],[5,6]]
Output: 11
Hints (1)
- Compute each row sum and take the maximum.
Solutions
Max of row sums. · Time O(rows*cols) · Space O(1)
func maxRowSum(matrix [][]int) int {
best := -1 << 62
for _, row := range matrix {
sum := 0
for _, v := range row {
sum += v
}
if sum > best {
best = sum
}
}
return best
}
Max of row sums. · Time O(rows*cols) · Space O(1)
function maxRowSum(matrix) {
return Math.max(...matrix.map(row => row.reduce((s, v) => s + v, 0)));
}
Max of row sums. · Time O(rows*cols) · Space O(1)
function maxRowSum($matrix) {
$best = null;
foreach ($matrix as $row) {
$sum = array_sum($row);
if ($best === null || $sum > $best) { $best = $sum; }
}
return $best;
}
Max of row sums. · Time O(rows*cols) · Space O(1)
def maxRowSum(matrix):
return max(sum(row) for row in matrix)
Max of row sums. · Time O(rows*cols) · Space O(1)
function maxRowSum(matrix: number[][]): number {
return Math.max(...matrix.map(row => row.reduce((s, v) => s + v, 0)));
}
Create a free account to submit solutions, track progress and save problems.
Create account