forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-number-of-moves-in-a-grid.cpp
92 lines (89 loc) · 2.61 KB
/
maximum-number-of-moves-in-a-grid.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
// Time: O(m * n)
// Space: O(m)
// dp
class Solution {
public:
int maxMoves(vector<vector<int>>& grid) {
vector<bool> dp(size(grid), true);
int c = 0;
for (; c < size(grid[0]) - 1; ++c) {
vector<bool> new_dp(size(grid));
for (int r = 0; r < size(grid); ++r) {
if (!dp[r]) {
continue;
}
if (grid[r][c] < grid[r][c + 1]) {
new_dp[r] = true;
}
if (r - 1 >= 0 && grid[r][c] < grid[r - 1][c + 1]) {
new_dp[r - 1] = true;
}
if (r + 1 < size(grid) && grid[r][c] < grid[r + 1][c + 1]) {
new_dp[r + 1] = true;
}
}
dp = move(new_dp);
if (!accumulate(cbegin(dp), cend(dp), 0)) {
break;
}
}
return c;
}
};
// Time: O(m * n)
// Space: O(m)
// dp
class Solution2 {
public:
int maxMoves(vector<vector<int>>& grid) {
vector<int> dp(size(grid));
for (int c = size(grid[0]) - 2; c >= 0; --c) {
vector<int> new_dp(size(grid));
for (int r = 0; r < size(grid); ++r) {
if (grid[r][c] < grid[r][c + 1]) {
new_dp[r] = max(new_dp[r], dp[r] + 1);
}
if (r - 1 >= 0 && grid[r][c] < grid[r - 1][c + 1]) {
new_dp[r] = max(new_dp[r], dp[r - 1] + 1);
}
if (r + 1 < size(grid) && grid[r][c] < grid[r + 1][c + 1]) {
new_dp[r] = max(new_dp[r], dp[r + 1] + 1);
}
}
dp = move(new_dp);
}
return *max_element(cbegin(dp), cend(dp));
}
};
// Time: O(m * n)
// Space: O(m)
// bfs
class Solution3 {
public:
int maxMoves(vector<vector<int>>& grid) {
unordered_set<int> q;
for (int r = 0; r < size(grid); ++r) {
q.emplace(r);
}
int c = 0;
for (; c < size(grid[0]) - 1; ++c) {
unordered_set<int> new_q;
for (const auto& r : q) {
if (grid[r][c] < grid[r][c + 1]) {
new_q.emplace(r);
}
if (r - 1 >= 0 && grid[r][c] < grid[r - 1][c + 1]) {
new_q.emplace(r - 1);
}
if (r + 1 < size(grid) && grid[r][c] < grid[r + 1][c + 1]) {
new_q.emplace(r + 1);
}
}
q = move(new_q);
if (empty(q)) {
break;
}
}
return c;
}
};