LeetCode解题报告(519)-- 2174. Remove All Ones With Row and Column Flips II
Posted onEdited onViews: Valine:
Problem
You are given a 0-indexedm x nbinary matrix grid.
In one operation, you can choose any i and j that meet the following conditions:
0 <= i < m
0 <= j < n
grid[i][j] == 1
and change the values of all cells in row i and column j to zero.
Return the minimum number of operations needed to remove all1‘s fromgrid.
Example 1:
1 2 3 4 5
Input: grid = [[1,1,1],[1,1,1],[0,1,0]] Output: 2 Explanation: In the first operation, change all cell values of row 1 and column 1 to zero. In the second operation, change all cell values of row 0 and column 0 to zero.
Example 2:
1 2 3 4 5 6
Input: grid = [[0,1,0],[1,0,1],[0,1,0]] Output: 2 Explanation: In the first operation, change all cell values of row 1 and column 0 to zero. In the second operation, change all cell values of row 2 and column 1 to zero. Note that we cannot perform an operation using row 1 and column 1 because grid[1][1] != 1.
Example 3:
1 2 3 4
Input: grid = [[0,0],[0,0]] Output: 0 Explanation: There are no 1's to remove so return 0.
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 15
1 <= m * n <= 15
grid[i][j] is either 0 or 1.
Analysis
这道题是Remove All Ones With Row and Column Flips的系列题,但是和之前这题的做法截然不同。前面提到了flipping的特点是只flip一次,flip两次都多余。但是这道题目是对所有的位置为1的格子,对其所在行和所在列变更为0(注意这里不是flip了,而是直接变成0),所以之前的性质在这里就不管用了。所以要变换思路,第一个提示是题目要求的是minimum operations,这种明显的提示一般就是BFS或者memorized searching,那我们就按照这个思路去做。
classSolution { public: intremoveOnes(vector<vector<int>>& grid){ memo = vector<int>(32768, INT_MAX); int state = 0; m = grid.size(), n = grid[0].size(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j]) { state |= (1 << (i * n + j)); } } } return dfs(state); } private: int m, n; vector<int> memo; intdfs(int state){ if (state == 0) { return0; } if (memo[state] != INT_MAX) { return memo[state]; } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (state & (1 << (i * n + j))) { int newState = state; for (int k = 0; k < m; ++k) { newState &= ~(1 << (k * n + j)); } for (int k = 0; k < n; ++k) { newState &= ~(1 << (i * n + k)); } memo[state] = min(memo[state], 1 + dfs(newState)); } } } return memo[state]; } };