Matrix
1380. Lucky Numbers in a Matrix
class Solution {
public:
vector<int> luckyNumbers(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<int> minRowVals(m, INT_MAX);
vector<int> maxColVals(n, INT_MIN);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
minRowVals[i] = std::min(minRowVals[i], matrix[i][j]);
maxColVals[j] = std::max(maxColVals[j], matrix[i][j]);
}
}
vector<int> res;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == minRowVals[i] && matrix[i][j] == maxColVals[j]) {
res.push_back(matrix[i][j]);
}
}
}
return res;
}
};832. Flipping an Image
For a square, the distance of any edge and the diagonal distance are the same. Calculate the distance of the edges and validate them equal and larger than 0.
Two rules:
A bunch of rectangles can form a Perfect Rectangle or not. So the sum of areas of multiple rectangles should be equals to the sum of Perfect Rectangle.
The 2nd one is For a Perfect Rectangle formed by multiple rectangles, the count 4 point objects(can easily get it from a single array) presence MUST be an EVEN number.
So we need a set to verify ALL point objects that we get for each iteration - if we store it already, remove it; if not, add into the set.
Finally, for a valid perfect rectangle, the set should contain ONLY 4 point object and which is the point object for the Perfect Rectangle.
Brute force
DP solution: O(m*m*n)
Stack Solution: Similar as histogram problem: O(m*n)

O(n^3)
Last updated
Was this helpful?