The Best Performing Section
A results grid holds one row per section of the class and one column per subject. The principal wants to congratulate the section whose marks add up to the most.
Return the row number of that section, counting from 0. If two sections tie, return the earlier one.
Examples
Example 1
Input: results = [[10, 10], [5, 30], [20, 5]]
Output: 1
The row totals are 10 + 10 = 20, 5 + 30 = 35 and 20 + 5 = 25. The largest is 35, which belongs to row 1 (rows are counted from 0).
Example 2
Input: results = [[7]]
Output: 0
Example 3
Input: results = [[1, 1], [2], [3, 3]]
Output: 2
Constraints
results has 1 to 200 rows, each with 1 to 200 marks between 0 and 100. Rows may have different lengths.
Return the index of the row with the largest total, not the total itself.
How to solve it
Hint
Add up each row as you meet it and keep the best total together with the row it came from.
Approach: Best row of a grid
- Keep the best total seen and the row it belongs to.
- For each row, add up its marks.
- If that total is strictly larger than the best so far, remember this row instead; return the remembered row.
Check with the example. [[10, 10], [5, 30], [20, 5]] has totals 20, 35, 25 — row 1 wins.
Complexity: O(r × c) time, O(1) space.
The reference solutions in all four languages are in the Solutions tab of the workspace below.
Item 61 of 90 · updated 2026-10-12