Suppose we have a binary matrix and another value k. You want to split the matrix into k pieces such that each piece contains at least one 1 in it. But there are some rules for cutting, we have to follow in order: 1. Select a direction: vertical or horizontal 2. Select an index in the matrix to cut into two sections. 3. If we cut vertically, we can no longer cut the left part but can only continue cutting the right part. 4. If we cut horizontally, we can no longer cut the top part and can only continue cutting the bottom part. So we have to find the number of different ways there are to divide the matrix. If the answer is very large, return result mod (10^9 + 7).
So, if the input is like
k = 2, then the output will be 4, as we can cut vertically twice and horizontally twice.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
from collections import defaultdict class Solution: def solve(self, matrix, k): p = 10 ** 9 + 7 m, n = len(matrix), len(matrix) counts = defaultdict(int) for i in range(m)[::-1]: for j in range(n)[::-1]: counts[(i, j)] = (counts[(i + 1, j)] + counts[(i, j + 1)] - counts[(i + 1, j + 1)] + matrix[i][j]) def f(x, y, c): count = counts[(x, y)] if c == 0: return 1 if count > 0 else 0 ans = 0 for i in range(x + 1, m): if 0 < counts[(i, y)] < count: ans += f(i, y, c - 1) for j in range(y + 1, n): if 0 < counts[(x, j)] < count: ans += f(x, j, c - 1) return ans % p return f(0, 0, k - 1) ob = Solution() matrix = [ [1, 1, 0], [1, 0, 1], [1, 1, 1], ] k = 2 print(ob.solve(matrix, k))
[ [1, 1, 0], [1, 0, 1], [1, 1, 1] ], 2