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 <= 105Loading editor...
[[-2,0,3]] 2