You are given a 2D integer array grid of size m x n, and an integer limit.
You may remove zero or more columns from the grid, but at least one column must remain. The relative order of the remaining columns must be preserved.
A grid is called consistent if for every row i, and for every pair of adjacent remaining columns a and b with a < b, the following holds: |grid[i][b] - grid[i][a]| <= limit.
Return the maximum number of columns that can remain such that the resulting grid is consistent.
Example 1:
Input: grid = [[-2,0,3]], limit = 2
Output: 2
Explanation:
|grid[0][1] − grid[0][0]| = |0 − (−2)| = 2 <= limit.Example 2:
Input: grid = [[1,-1,1],[2,2,2]], limit = 1
Output: 2
Explanation:
|grid[0][2] − grid[0][0]| = |1 − 1| = 0 <= limit and|grid[1][2] − grid[1][0]| = |2 − 2| = 0 <= limit.Example 3:
Input: grid = [[-5,5]], limit = 9
Output: 1
Explanation:
|grid[0][1] − grid[0][0]| = |5 − (−5)| = 10 > limit.
Constraints:
1 <= m == grid.length <= 2501 <= n == grid[i].length <= 250-105 <= grid[i][j] <= 1050 <= limit <= 105Problem Overview: You need to find the maximum number of columns in a grid that can be treated as consistent under the problem’s row and column constraints. The core challenge is recognizing that columns with the same structural pattern behave identically, even when values are transformed or normalized.
Approach 1: Brute Force Column Comparison (Time: O(m * n2), Space: O(1))
The direct approach compares every pair of columns cell by cell. For each column, iterate through all remaining columns and verify whether their values satisfy the consistency rule across every row. This works for small grids because it does not require additional preprocessing or auxiliary structures. The downside is the quadratic comparison cost across columns, which becomes expensive when n is large.
Approach 2: Column Signature Hashing (Time: O(m * n), Space: O(n))
The optimized solution converts every column into a normalized signature and stores the frequency in a hash map. You iterate through each column, build a compact representation using its row values, and use hash lookup to count matching patterns in constant time. The key insight is that consistent columns share the same normalized structure, so repeated comparisons are unnecessary. This approach is the standard interview solution because it reduces repeated work and scales efficiently for dense grids.
Instead of comparing columns repeatedly, you preprocess each column once and group equivalent columns together. In most implementations, the signature is stored as a string, tuple, or bitmask depending on language constraints. Using a bitmask can further reduce memory overhead when the grid is binary. Problems built around pattern grouping often rely on hash maps, bit manipulation, and lightweight array traversal.
Approach 3: Bitmask Compression (Time: O(m * n), Space: O(n))
When the grid contains only binary values, each column can be compressed into an integer bitmask. As you iterate through rows, shift bits into the mask and use the final integer as the hash key. This avoids string construction costs and improves cache efficiency in lower-level languages like C++ and Java. The algorithmic complexity stays linear, but the implementation becomes more optimized for competitive programming constraints.
Recommended for interviews: Start with the brute force comparison to demonstrate that you understand the consistency condition. Then move to the hash-based grouping approach, since interviewers usually expect you to eliminate repeated column scans and reduce the complexity to O(m * n). If constraints are tight and the grid is binary, mentioning bitmask compression shows strong optimization awareness.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Column Comparison | O(m * n²) | O(1) | Small grids or when optimizing is unnecessary |
| Hash Map with Column Signatures | O(m * n) | O(n) | General case and interview-preferred solution |
| Bitmask Compression | O(m * n) | O(n) | Binary grids with tight memory or performance constraints |
Leetcode 3989 | Maximum Consistent Columns in a Grid | Dynamic Programming | Weekly contest 510 • CodeWithMeGuys • 231 views views
Watch 4 more video solutions →Practice Maximum Consistent Columns in a Grid with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor