You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/3100-3199/3148.Maximum Difference Score in a Grid/README_EN.md
+116-4
Original file line number
Diff line number
Diff line change
@@ -51,24 +51,136 @@ The total score is <code>2 + 7 = 9</code>.</p>
51
51
52
52
## Solutions
53
53
54
-
### Solution 1
54
+
### Solution 1: Dynamic Programming
55
+
56
+
According to the problem description, if the values of the cells we pass through are $c_1, c_2, \cdots, c_k$, then our score is $c_2 - c_1 + c_3 - c_2 + \cdots + c_k - c_{k-1} = c_k - c_1$. Therefore, the problem is transformed into: for each cell $(i, j)$ of the matrix, if we take it as the endpoint, what is the minimum value of the starting point.
57
+
58
+
We can use dynamic programming to solve this problem. We define $f[i][j]$ as the minimum value of the path with $(i, j)$ as the endpoint. Then we can get the state transition equation:
59
+
60
+
$$
61
+
f[i][j] = \min(f[i-1][j], f[i][j-1], grid[i][j])
62
+
$$
63
+
64
+
So the answer is the maximum value of $\text{grid}[i][j] - \min(f[i-1][j], f[i][j-1])$.
65
+
66
+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the matrix, respectively.
55
67
56
68
<!-- tabs:start -->
57
69
58
70
```python
59
-
71
+
classSolution:
72
+
defmaxScore(self, grid: List[List[int]]) -> int:
73
+
f = [[0] *len(grid[0]) for _ inrange(len(grid))]
74
+
ans =-inf
75
+
for i, row inenumerate(grid):
76
+
for j, x inenumerate(row):
77
+
mi = inf
78
+
if i:
79
+
mi =min(mi, f[i -1][j])
80
+
if j:
81
+
mi =min(mi, f[i][j -1])
82
+
ans =max(ans, x - mi)
83
+
f[i][j] =min(x, mi)
84
+
return ans
60
85
```
61
86
62
87
```java
63
-
88
+
classSolution {
89
+
publicintmaxScore(List<List<Integer>>grid) {
90
+
int m = grid.size(), n = grid.get(0).size();
91
+
finalint inf =1<<30;
92
+
int ans =-inf;
93
+
int[][] f =newint[m][n];
94
+
for (int i =0; i < m; ++i) {
95
+
for (int j =0; j < n; ++j) {
96
+
int mi = inf;
97
+
if (i >0) {
98
+
mi =Math.min(mi, f[i -1][j]);
99
+
}
100
+
if (j >0) {
101
+
mi =Math.min(mi, f[i][j -1]);
102
+
}
103
+
ans =Math.max(ans, grid.get(i).get(j) - mi);
104
+
f[i][j] =Math.min(grid.get(i).get(j), mi);
105
+
}
106
+
}
107
+
return ans;
108
+
}
109
+
}
64
110
```
65
111
66
112
```cpp
67
-
113
+
classSolution {
114
+
public:
115
+
int maxScore(vector<vector<int>>& grid) {
116
+
int m = grid.size(), n = grid[0].size();
117
+
const int inf = 1 << 30;
118
+
int ans = -inf;
119
+
int f[m][n];
120
+
for (int i = 0; i < m; ++i) {
121
+
for (int j = 0; j < n; ++j) {
122
+
int mi = inf;
123
+
if (i) {
124
+
mi = min(mi, f[i - 1][j]);
125
+
}
126
+
if (j) {
127
+
mi = min(mi, f[i][j - 1]);
128
+
}
129
+
ans = max(ans, grid[i][j] - mi);
130
+
f[i][j] = min(grid[i][j], mi);
131
+
}
132
+
}
133
+
return ans;
134
+
}
135
+
};
68
136
```
69
137
70
138
```go
139
+
func maxScore(grid [][]int) int {
140
+
m, n := len(grid), len(grid[0])
141
+
f := make([][]int, m)
142
+
for i := range f {
143
+
f[i] = make([]int, n)
144
+
}
145
+
const inf int = 1 << 30
146
+
ans := -inf
147
+
for i, row := range grid {
148
+
for j, x := range row {
149
+
mi := inf
150
+
if i > 0 {
151
+
mi = min(mi, f[i-1][j])
152
+
}
153
+
if j > 0 {
154
+
mi = min(mi, f[i][j-1])
155
+
}
156
+
ans = max(ans, x-mi)
157
+
f[i][j] = min(x, mi)
158
+
}
159
+
}
160
+
return ans
161
+
}
162
+
```
71
163
164
+
```ts
165
+
function maxScore(grid:number[][]):number {
166
+
const [m, n] = [grid.length, grid[0].length];
167
+
const f:number[][] =Array.from({ length: m }, () =>Array.from({ length: n }, () =>0));
0 commit comments