Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed 2D integer array nums. Initially, your score is 0. Perform the following operations until the matrix becomes empty:
Return the final score.
Example 1:
Input: nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]] Output: 15 Explanation: In the first operation, we remove 7, 6, 6, and 3. We then add 7 to our score. Next, we remove 2, 4, 5, and 2. We add 5 to our score. Lastly, we remove 1, 2, 3, and 1. We add 3 to our score. Thus, our final score is 7 + 5 + 3 = 15.
Example 2:
Input: nums = [[1]] Output: 1 Explanation: We remove 1 and add it to the answer. We return 1.
Constraints:
1 <= nums.length <= 3001 <= nums[i].length <= 5000 <= nums[i][j] <= 103Problem summary: You are given a 0-indexed 2D integer array nums. Initially, your score is 0. Perform the following operations until the matrix becomes empty: From each row in the matrix, select the largest number and remove it. In the case of a tie, it does not matter which number is chosen. Identify the highest number amongst all those removed in step 1. Add that number to your score. Return the final score.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[7,2,1],[6,4,2],[6,5,3],[3,2,1]]
[[1]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2679: Sum in a Matrix
class Solution {
public int matrixSum(int[][] nums) {
for (var row : nums) {
Arrays.sort(row);
}
int ans = 0;
for (int j = 0; j < nums[0].length; ++j) {
int mx = 0;
for (var row : nums) {
mx = Math.max(mx, row[j]);
}
ans += mx;
}
return ans;
}
}
// Accepted solution for LeetCode #2679: Sum in a Matrix
func matrixSum(nums [][]int) (ans int) {
for _, row := range nums {
sort.Ints(row)
}
for i := 0; i < len(nums[0]); i++ {
mx := 0
for _, row := range nums {
mx = max(mx, row[i])
}
ans += mx
}
return
}
# Accepted solution for LeetCode #2679: Sum in a Matrix
class Solution:
def matrixSum(self, nums: List[List[int]]) -> int:
for row in nums:
row.sort()
return sum(map(max, zip(*nums)))
// Accepted solution for LeetCode #2679: Sum in a Matrix
impl Solution {
pub fn matrix_sum(mut nums: Vec<Vec<i32>>) -> i32 {
for row in &mut nums {
row.sort();
}
(0..nums[0].len())
.map(|col| nums.iter().map(|row| row[col]).max().unwrap())
.sum()
}
}
// Accepted solution for LeetCode #2679: Sum in a Matrix
function matrixSum(nums: number[][]): number {
for (const row of nums) {
row.sort((a, b) => a - b);
}
let ans = 0;
for (let j = 0; j < nums[0].length; ++j) {
let mx = 0;
for (const row of nums) {
mx = Math.max(mx, row[j]);
}
ans += mx;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.