Maximum Connected Component Sum in a Grid | AlgoBlazer
Maximum Connected Component Sum in a Grid
SilverShow difficulty
10000ms500MBSep 20, 2026
You are given a grid of non-negative integers with m rows and n columns. A cell with value 0 is blocked. A cell with a positive value belongs to a component.
A component is a maximal group of positive cells connected 4-directionally (up, down, left, or right). The sum of a component is the total of its cell values.
Return the largest component sum in the grid, or 0 if every cell is 0.
Examples
Example 1
Input: grid = [[0, 2, 1, 0],
[4, 0, 0, 3],
[1, 0, 0, 4],
[0, 3, 2, 0]]
Output: 7
Explanation: The largest connected group of positive cells has values 3 and 4, for a total of 7.
Example 2
Input: grid = [[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1]]
Output: 1
Explanation: Two separate single-cell components each have value 1, so the largest sum is 1.
Constraints
1≤m,n≤10 where m and n are the number of rows and columns of grid
0≤grid[i][j]≤10
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up