I have written a code to count group of 1's in binary matrix. Referring to my question link here
code
def groupcheck(i, j, matrix):
if 0 <= i < len(matrix) and 0 <= j < len(matrix):
if matrix[i][j]:
matrix[i][j] = 0
for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
groupcheck(i + dx, j + dy, matrix)
def countGroup(matrix):
count = 0;
for i in range(len(matrix)):
for j in range(len(matrix)):
if matrix[i][j]:
count += 1
groupcheck(i, j, matrix)
return count
matrix = [
[1,1,0,0],
[1,1,1,0],
[0,1,1,0],
[0,0,0,1]
]
group = countGroup(matrix)
print(group)
Can someone please help me to calculate the complexity of this algorithm and what kind of approach it is? Also is there any better approach than this?
As per me complexity and approach(please correct me if I'm wrong):
complexity : O(n^2*4) (n is length of square matrix)
approach: brute force
I'm still learning please try to explain me if possible.