-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1301.cpp
More file actions
executable file
·42 lines (37 loc) · 1019 Bytes
/
LC1301.cpp
File metadata and controls
executable file
·42 lines (37 loc) · 1019 Bytes
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
/*
Problem Statement: https://leetcode.com/problems/number-of-paths-with-max-score/
*/
class Solution {
public:
vector<int> pathsWithMaxScore(vector<string>& board) {
int mod, n, val;
mod = 1e9 + 7;
n = board.size();
vector< vector<int> > score(n + 1, vector<int>(n + 1)), paths(n + 1, vector<int>(n + 1));
// Helper function
auto process = [&](int u, int v, int i, int j) {
if (score[u][v] < score[i][j]) {
score[u][v] = score[i][j];
paths[u][v] = paths[i][j];
}
else if (score[u][v] == score[i][j])
paths[u][v] = (paths[u][v] + paths[i][j]) % mod;
};
// Initialize
paths[0][0] = 1;
board[0][0] = board[n - 1][n - 1] = '0';
// Dynamic Programming
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
val = board[i - 1][j - 1] - '0';
if (val > 9)
continue;
process(i, j, i - 1, j);
process(i, j, i, j - 1);
process(i, j, i - 1, j - 1);
if (paths[i][j])
score[i][j] += val;
}
return {score[n][n], paths[n][n]};
}
};