Count Submatrices with Top-Left Element and Sum Less Than k - Complete Solution Guide
Count Submatrices with Top-Left Element and Sum Less Than k is LeetCode problem 3070, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 0-indexed integer matrix grid and an integer k . Return the number of submatrices that contain the top-left element of the grid , and have a sum less than or equal to k . Example 1: Input: grid = [[7,6,3],[6,6,1]], k = 18 Output: 4 Explanation: There are only 4 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 18. Example 2: Input: grid = [[7,2,9],[1,5,0],[2,6,6]], k = 20 Output: 6 Explanation: There are only 6
Detailed Explanation
The problem asks us to count the number of submatrices within a given matrix `grid` that satisfy two conditions: (1) the submatrix must include the top-left element of the `grid`, and (2) the sum of all elements within the submatrix must be less than or equal to a given integer `k`. The input is the `grid` (a 2D array of integers) and the integer `k`. The output is the total count of submatrices meeting both conditions. The matrix dimensions are limited to 1000x1000, and element values are between 0 and 1000, with `k` being between 1 and 10^9.
Solution Approach
The solution uses dynamic programming with prefix sums to efficiently calculate the sum of submatrices. First, it calculates the row-wise prefix sum. Then, it calculates the column-wise prefix sum on the row-wise prefix sum. For each possible submatrix defined by its bottom-right corner (r, c), the sum of its elements can be obtained from the prefix sum matrix. The code iterates through all possible bottom-right corners of submatrices that include the top-left element, and for each submatrix, it checks if the sum of its elements is less than or equal to `k`. If it is, the counter is incremented. A key optimization is that if, for a particular column, the sum exceeds `k`, we can break the inner loop because any submatrix extending further down in that column will also have a sum greater than `k`.
Step-by-Step Algorithm
- Step 1: Iterate through each row of the grid (from row 0 to m-1).
- Step 2: For each row, iterate through each column (from column 1 to n-1).
- Step 3: Calculate the row-wise prefix sum: `grid[r][c] += grid[r][c-1]`. This transforms each element to represent the sum of elements from the beginning of that row until that column.
- Step 4: Initialize a counter variable `count` to 0. This will store the number of submatrices meeting the criteria.
- Step 5: Iterate through each column of the grid (from column 0 to n-1).
- Step 6: For each column, iterate through each row (from row 0 to m-1).
- Step 7: Calculate column-wise prefix sum on top of the row-wise prefix sum. if (r > 0) { grid[r][c] += grid[r-1][c]; }. This ensures grid[r][c] represents the sum of the submatrix from (0,0) to (r,c).
- Step 8: Check if the sum of the submatrix (represented by `grid[r][c]`) is less than or equal to `k`.
- Step 9: If the sum is less than or equal to `k`, increment the `count`.
- Step 10: If the sum is greater than `k`, break the inner loop (row iteration) because all submatrices extending further down in that column will also have a sum greater than `k`.
- Step 11: After iterating through all possible submatrices, return the `count`.
Key Insights
- Insight 1: The problem can be efficiently solved by utilizing prefix sums to quickly calculate the sum of elements within any submatrix originating from the top-left corner.
- Insight 2: The condition that the submatrix must contain the top-left element simplifies the problem because it reduces the search space significantly. We only need to consider submatrices with top-left at (0,0).
- Insight 3: By calculating prefix sums in-place, we can avoid using extra space, optimizing the space complexity of the solution.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(1)
Topics
This problem involves: Array, Matrix, Prefix Sum.
Companies
Asked at: Barclays.