-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily134.cpp
More file actions
148 lines (121 loc) · 5.53 KB
/
Copy pathdaily134.cpp
File metadata and controls
148 lines (121 loc) · 5.53 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Solution 1 - FAIL
class Solution {
public:
int maxMoves(vector<vector<int>>& grid) {
/*
dp, ith row jth column gives max number of moves to this grid
*/
// Initialize DP table with the same size as grid, filled with 0s.
auto moves = std::vector<std::vector<int>>(grid.size(), std::vector<int>(grid[0].size(), 0));
// Fill DP table from left to right, calculating max moves possible to each cell.
for (int j = 0; j < grid[0].size() - 1; ++j) {
for (int i = 0; i < grid.size(); ++i) {
// Move to (i+1, j+1) if valid, and next cell is greater than current cell
if (i + 1 < grid.size() && grid[i + 1][j + 1] > grid[i][j]) {
moves[i + 1][j + 1] = std::max(moves[i + 1][j + 1], moves[i][j] + 1);
}
// Move to (i, j+1) if next cell is greater than current cell
if (grid[i][j + 1] > grid[i][j]) {
moves[i][j + 1] = std::max(moves[i][j + 1], moves[i][j] + 1);
}
// Move to (i-1, j+1) if valid, and next cell is greater than current cell
if (i - 1 >= 0 && grid[i - 1][j + 1] > grid[i][j]) {
moves[i - 1][j + 1] = std::max(moves[i - 1][j + 1], moves[i][j] + 1);
}
}
}
// Find the maximum moves possible from any cell in the last column.
int max_move = 0;
for (int i = 0; i < grid.size(); ++i) {
max_move = std::max(max_move, moves[i][grid[0].size() - 1]);
}
return max_move;
}
};
// Solution 2
class Solution {
public:
int maxMoves(vector<vector<int>>& grid) {
// Get dimensions of the grid
int m = grid.size(); // number of rows
int n = grid[0].size(); // number of columns
// res will store the rightmost column we can reach
int res = 0;
// dp array stores the maximum number of moves possible to reach each cell
// in the current column we're processing
vector<int> dp(m);
// Iterate through each column from left to right (starting from column 1)
for (int j = 1; j < n; ++j) {
// leftTop keeps track of the dp value from the cell above-left
int leftTop = 0;
// found indicates if we can reach any cell in current column
bool found = false;
// Iterate through each row in current column
for (int i = 0; i < m; ++i) {
// cur will store the maximum moves to reach current cell
int cur = -1;
// Store dp[i] for next iteration's leftTop
int nxtLeftTop = dp[i];
// Check move from top-left cell (if valid)
if (i - 1 >= 0 && leftTop != -1 && grid[i][j] > grid[i - 1][j - 1]) {
cur = max(cur, leftTop + 1);
}
// Check move from direct left cell (if valid)
if (dp[i] != -1 && grid[i][j] > grid[i][j - 1]) {
cur = max(cur, dp[i] + 1);
}
// Check move from bottom-left cell (if valid)
if (i + 1 < m && dp[i + 1] != -1 && grid[i][j] > grid[i + 1][j - 1]) {
cur = max(cur, dp[i + 1] + 1);
}
// Update dp array for current cell
dp[i] = cur;
// Update found flag if we can reach current cell
found = found || (dp[i] != -1);
// Update leftTop for next row's iteration
leftTop = nxtLeftTop;
}
// If we can't reach any cell in current column, break
if (!found) break;
// Update result to current column if we can reach it
res = j;
}
// Return the maximum number of moves possible
return res;
}
};
// Solution 3
class Solution {
public:
int maxMoves(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
// Memoization table for max moves from each cell
std::vector<std::vector<int>> moves(m, std::vector<int>(n, -1));
// Helper function to perform DFS with memoization
auto dfs = [&](auto&& self, int i, int j) -> int {
// If out of bounds, return 0 moves possible
if (j == n - 1) return 0; // Last column reached
if (moves[i][j] != -1) return moves[i][j]; // Return cached result
int max_move = 0;
// Check all valid moves to the next column
if (i > 0 && grid[i][j] < grid[i - 1][j + 1]) {
max_move = std::max(max_move, 1 + self(self, i - 1, j + 1));
}
if (grid[i][j] < grid[i][j + 1]) {
max_move = std::max(max_move, 1 + self(self, i, j + 1));
}
if (i + 1 < m && grid[i][j] < grid[i + 1][j + 1]) {
max_move = std::max(max_move, 1 + self(self, i + 1, j + 1));
}
moves[i][j] = max_move; // Cache the result
return max_move;
};
// Find maximum moves possible starting from any cell in the first column
int max_result = 0;
for (int i = 0; i < m; ++i) {
max_result = std::max(max_result, dfs(dfs, i, 0));
}
return max_result;
}
};