-
Notifications
You must be signed in to change notification settings - Fork 3
/
2713.cpp
40 lines (34 loc) · 1.17 KB
/
2713.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
typedef pair<int, pair<int, int>> P;
class Solution {
public:
int maxIncreasingCells(vector<vector<int>>& mat) {
int m = mat.size();
int n = mat[0].size();
vector<P> nums;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
nums.push_back({mat[i][j], {i, j}});
}
}
sort(nums.begin(), nums.end());
vector<map<int, int>> rows(m);
vector<map<int, int>> cols(n);
for (int i = 0; i < m; ++i) rows[i][INT_MIN] = 0;
for (int i = 0; i < n; ++i) cols[i][INT_MIN] = 0;
int res = 0;
for (auto& [num, pos] : nums) {
int length = 1;
auto [x, y] = pos;
auto iter = rows[x].lower_bound(num);
iter--;
length = max(length, 1 + iter->second);
iter = cols[y].lower_bound(num);
iter--;
length = max(length, 1 + iter->second);
res = max(res, length);
rows[x][num] = max(length, rows[x][num]);
cols[y][num] = max(length, cols[y][num]);
}
return res;
}
};