Task 1: Highest Row
"Row by Row: Which Sum Stands Tallest?"
Given an m x n matrix, find the row with the largest sum and return that sum.
The Strategy: Iterate over each row, sum its elements, and track the maximum. Simple linear scan through all matrix elements in O(m*n).
Perl Implementation
sub highest_row {
my ($matrix) = @_;
return 0 unless defined $matrix && @$matrix > 0;
my $max_sum = 0;
foreach my $row (@$matrix) {
my $row_sum = 0;
$row_sum += $_ for @$row;
$max_sum = $row_sum if $row_sum > $max_sum;
}
return $max_sum;
}
Python Implementation
def highest_row(matrix: list[list[int]]) -> int:
"""Return the largest row sum in the matrix."""
if not matrix:
return 0
return max(sum(row) for row in matrix)