-
Notifications
You must be signed in to change notification settings - Fork 257
/
longest-increasing-continuous-subsequence-ii.cpp
115 lines (98 loc) · 3.46 KB
/
longest-increasing-continuous-subsequence-ii.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Time: O(m * n)
// Space: O(m * n)
class Solution {
public:
/**
* @param A an integer matrix
* @return an integer
*/
int longestIncreasingContinuousSubsequenceII(vector<vector<int>>& A) {
if (A.empty()) {
return 0;
}
// max_inc_len[i][j] stores the length of longest increasing continuous
// subsequence which starts with A[i][j]
vector<vector<int>> max_inc_len(A.size(), vector<int>(A[0].size(), 0));
int ans = 0;
for (int i = 0; i < A.size(); ++i) {
for (int j = 0; j < A[0].size(); ++j) {
// Not yet visited.
if (max_inc_len[i][j] == 0) {
ans = max(ans, fill(A, i, j, INT_MIN, max_inc_len));
}
}
}
return ans;
}
int fill(const vector<vector<int>>& A, const int i, const int j,
const int prev_val,
vector<vector<int>>& max_inc_len) {
// Invalid cases.
if (i < 0 || i >= A.size() || j < 0 || j >= A[0].size() ||
A[i][j] <= prev_val) {
return 0;
}
// Return max_inc_len if visited.
if (max_inc_len[i][j] > 0) {
return max_inc_len[i][j];
}
// Try each direction to find the max of max_inc_len[i][j].
const vector<pair<int, int>> directions = {{0, 1}, {0, -1},
{1, 0}, {-1, 0}};
for (const auto& d : directions) {
max_inc_len[i][j] = max(max_inc_len[i][j],
1 + fill(A, i + d.first, j + d.second,
A[i][j], max_inc_len));
}
return max_inc_len[i][j];
}
};
// Time: O(m * n)
// Space: O(m * n)
class Solution2 {
public:
/**
* @param A an integer matrix
* @return an integer
*/
int longestIncreasingContinuousSubsequenceII(vector<vector<int>>& A) {
if (A.empty()) {
return 0;
}
// max_inc_len[i][j] stores the length of longest decreasing continuous
// subsequence which starts with A[i][j]
vector<vector<int>> max_inc_len(A.size(), vector<int>(A[0].size(), 0));
int ans = 0;
for (int i = 0; i < A.size(); ++i) {
for (int j = 0; j < A[0].size(); ++j) {
// Not yet visited.
if (max_inc_len[i][j] == 0) {
ans = max(ans, fill(A, i, j, INT_MAX, max_inc_len));
}
}
}
return ans;
}
int fill(const vector<vector<int>>& A, const int i, const int j,
const int prev_val,
vector<vector<int>>& max_inc_len) {
// Invalid cases.
if (i < 0 || i >= A.size() || j < 0 || j >= A[0].size() ||
A[i][j] >= prev_val) {
return 0;
}
// Return max_inc_len if visited.
if (max_inc_len[i][j] > 0) {
return max_inc_len[i][j];
}
// Try each direction to find the max of max_inc_len[i][j].
const vector<pair<int, int>> directions = {{0, 1}, {0, -1},
{1, 0}, {-1, 0}};
for (const auto& d : directions) {
max_inc_len[i][j] = max(max_inc_len[i][j],
1 + fill(A, i + d.first, j + d.second,
A[i][j], max_inc_len));
}
return max_inc_len[i][j];
}
};