DevPath

← All problems

Maximum Row Sum

Easy matrix

Description

Return the largest sum among all rows.

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
}

Create a free account to submit solutions, track progress and save problems.

Create account