From 1600de954af9536b6460435ce6cf807dd8a3d50f Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 13 May 2024 10:57:43 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3148 No.3148.Maximum Difference Score in a Grid --- .../README.md | 120 +++++++++++++++++- .../README_EN.md | 120 +++++++++++++++++- .../Solution.cpp | 23 ++++ .../Solution.go | 23 ++++ .../Solution.java | 22 ++++ .../Solution.py | 14 ++ .../Solution.ts | 19 +++ 7 files changed, 333 insertions(+), 8 deletions(-) create mode 100644 solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.cpp create mode 100644 solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.go create mode 100644 solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.java create mode 100644 solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.py create mode 100644 solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.ts diff --git a/solution/3100-3199/3148.Maximum Difference Score in a Grid/README.md b/solution/3100-3199/3148.Maximum Difference Score in a Grid/README.md index a3462d4561f7d..496b80388d5fa 100644 --- a/solution/3100-3199/3148.Maximum Difference Score in a Grid/README.md +++ b/solution/3100-3199/3148.Maximum Difference Score in a Grid/README.md @@ -55,24 +55,136 @@ ## 解法 -### 方法一 +### 方法一:动态规划 + +根据题目描述,如果我们经过的单元格的值依次是 $c_1, c_2, \cdots, c_k$,那么我们的得分就是 $c_2 - c_1 + c_3 - c_2 + \cdots + c_k - c_{k-1} = c_k - c_1$。因此,问题转化为:对于矩阵的每个单元格 $(i, j)$,如果我们将其作为终点,那么起点的最小值是多少。 + +我们可以使用动态规划来解决这个问题。我们定义 $f[i][j]$ 表示以 $(i, j)$ 为终点的路径的最小值。那么我们可以得到状态转移方程: + +$$ +f[i][j] = \min(f[i-1][j], f[i][j-1], grid[i][j]) +$$ + +那么答案为 $\text{grid}[i][j] - \min(f[i-1][j], f[i][j-1])$ 的最大值。 + +时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 ```python - +class Solution: + def maxScore(self, grid: List[List[int]]) -> int: + f = [[0] * len(grid[0]) for _ in range(len(grid))] + ans = -inf + for i, row in enumerate(grid): + for j, x in enumerate(row): + mi = inf + if i: + mi = min(mi, f[i - 1][j]) + if j: + mi = min(mi, f[i][j - 1]) + ans = max(ans, x - mi) + f[i][j] = min(x, mi) + return ans ``` ```java - +class Solution { + public int maxScore(List> grid) { + int m = grid.size(), n = grid.get(0).size(); + final int inf = 1 << 30; + int ans = -inf; + int[][] f = new int[m][n]; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + int mi = inf; + if (i > 0) { + mi = Math.min(mi, f[i - 1][j]); + } + if (j > 0) { + mi = Math.min(mi, f[i][j - 1]); + } + ans = Math.max(ans, grid.get(i).get(j) - mi); + f[i][j] = Math.min(grid.get(i).get(j), mi); + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int maxScore(vector>& grid) { + int m = grid.size(), n = grid[0].size(); + const int inf = 1 << 30; + int ans = -inf; + int f[m][n]; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + int mi = inf; + if (i) { + mi = min(mi, f[i - 1][j]); + } + if (j) { + mi = min(mi, f[i][j - 1]); + } + ans = max(ans, grid[i][j] - mi); + f[i][j] = min(grid[i][j], mi); + } + } + return ans; + } +}; ``` ```go +func maxScore(grid [][]int) int { + m, n := len(grid), len(grid[0]) + f := make([][]int, m) + for i := range f { + f[i] = make([]int, n) + } + const inf int = 1 << 30 + ans := -inf + for i, row := range grid { + for j, x := range row { + mi := inf + if i > 0 { + mi = min(mi, f[i-1][j]) + } + if j > 0 { + mi = min(mi, f[i][j-1]) + } + ans = max(ans, x-mi) + f[i][j] = min(x, mi) + } + } + return ans +} +``` +```ts +function maxScore(grid: number[][]): number { + const [m, n] = [grid.length, grid[0].length]; + const f: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => 0)); + let ans = -Infinity; + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + let mi = Infinity; + if (i) { + mi = Math.min(mi, f[i - 1][j]); + } + if (j) { + mi = Math.min(mi, f[i][j - 1]); + } + ans = Math.max(ans, grid[i][j] - mi); + f[i][j] = Math.min(mi, grid[i][j]); + } + } + return ans; +} ``` diff --git a/solution/3100-3199/3148.Maximum Difference Score in a Grid/README_EN.md b/solution/3100-3199/3148.Maximum Difference Score in a Grid/README_EN.md index 6714cbc3041b8..adb4e07ccf6ff 100644 --- a/solution/3100-3199/3148.Maximum Difference Score in a Grid/README_EN.md +++ b/solution/3100-3199/3148.Maximum Difference Score in a Grid/README_EN.md @@ -51,24 +51,136 @@ The total score is 2 + 7 = 9.

## Solutions -### Solution 1 +### Solution 1: Dynamic Programming + +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. + +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: + +$$ +f[i][j] = \min(f[i-1][j], f[i][j-1], grid[i][j]) +$$ + +So the answer is the maximum value of $\text{grid}[i][j] - \min(f[i-1][j], f[i][j-1])$. + +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. ```python - +class Solution: + def maxScore(self, grid: List[List[int]]) -> int: + f = [[0] * len(grid[0]) for _ in range(len(grid))] + ans = -inf + for i, row in enumerate(grid): + for j, x in enumerate(row): + mi = inf + if i: + mi = min(mi, f[i - 1][j]) + if j: + mi = min(mi, f[i][j - 1]) + ans = max(ans, x - mi) + f[i][j] = min(x, mi) + return ans ``` ```java - +class Solution { + public int maxScore(List> grid) { + int m = grid.size(), n = grid.get(0).size(); + final int inf = 1 << 30; + int ans = -inf; + int[][] f = new int[m][n]; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + int mi = inf; + if (i > 0) { + mi = Math.min(mi, f[i - 1][j]); + } + if (j > 0) { + mi = Math.min(mi, f[i][j - 1]); + } + ans = Math.max(ans, grid.get(i).get(j) - mi); + f[i][j] = Math.min(grid.get(i).get(j), mi); + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int maxScore(vector>& grid) { + int m = grid.size(), n = grid[0].size(); + const int inf = 1 << 30; + int ans = -inf; + int f[m][n]; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + int mi = inf; + if (i) { + mi = min(mi, f[i - 1][j]); + } + if (j) { + mi = min(mi, f[i][j - 1]); + } + ans = max(ans, grid[i][j] - mi); + f[i][j] = min(grid[i][j], mi); + } + } + return ans; + } +}; ``` ```go +func maxScore(grid [][]int) int { + m, n := len(grid), len(grid[0]) + f := make([][]int, m) + for i := range f { + f[i] = make([]int, n) + } + const inf int = 1 << 30 + ans := -inf + for i, row := range grid { + for j, x := range row { + mi := inf + if i > 0 { + mi = min(mi, f[i-1][j]) + } + if j > 0 { + mi = min(mi, f[i][j-1]) + } + ans = max(ans, x-mi) + f[i][j] = min(x, mi) + } + } + return ans +} +``` +```ts +function maxScore(grid: number[][]): number { + const [m, n] = [grid.length, grid[0].length]; + const f: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => 0)); + let ans = -Infinity; + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + let mi = Infinity; + if (i) { + mi = Math.min(mi, f[i - 1][j]); + } + if (j) { + mi = Math.min(mi, f[i][j - 1]); + } + ans = Math.max(ans, grid[i][j] - mi); + f[i][j] = Math.min(mi, grid[i][j]); + } + } + return ans; +} ``` diff --git a/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.cpp b/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.cpp new file mode 100644 index 0000000000000..f5279402d1922 --- /dev/null +++ b/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int maxScore(vector>& grid) { + int m = grid.size(), n = grid[0].size(); + const int inf = 1 << 30; + int ans = -inf; + int f[m][n]; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + int mi = inf; + if (i) { + mi = min(mi, f[i - 1][j]); + } + if (j) { + mi = min(mi, f[i][j - 1]); + } + ans = max(ans, grid[i][j] - mi); + f[i][j] = min(grid[i][j], mi); + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.go b/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.go new file mode 100644 index 0000000000000..1b5aa8874ca45 --- /dev/null +++ b/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.go @@ -0,0 +1,23 @@ +func maxScore(grid [][]int) int { + m, n := len(grid), len(grid[0]) + f := make([][]int, m) + for i := range f { + f[i] = make([]int, n) + } + const inf int = 1 << 30 + ans := -inf + for i, row := range grid { + for j, x := range row { + mi := inf + if i > 0 { + mi = min(mi, f[i-1][j]) + } + if j > 0 { + mi = min(mi, f[i][j-1]) + } + ans = max(ans, x-mi) + f[i][j] = min(x, mi) + } + } + return ans +} \ No newline at end of file diff --git a/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.java b/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.java new file mode 100644 index 0000000000000..6fc2cc0c7261c --- /dev/null +++ b/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.java @@ -0,0 +1,22 @@ +class Solution { + public int maxScore(List> grid) { + int m = grid.size(), n = grid.get(0).size(); + final int inf = 1 << 30; + int ans = -inf; + int[][] f = new int[m][n]; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + int mi = inf; + if (i > 0) { + mi = Math.min(mi, f[i - 1][j]); + } + if (j > 0) { + mi = Math.min(mi, f[i][j - 1]); + } + ans = Math.max(ans, grid.get(i).get(j) - mi); + f[i][j] = Math.min(grid.get(i).get(j), mi); + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.py b/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.py new file mode 100644 index 0000000000000..bb2ae285c3e6f --- /dev/null +++ b/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def maxScore(self, grid: List[List[int]]) -> int: + f = [[0] * len(grid[0]) for _ in range(len(grid))] + ans = -inf + for i, row in enumerate(grid): + for j, x in enumerate(row): + mi = inf + if i: + mi = min(mi, f[i - 1][j]) + if j: + mi = min(mi, f[i][j - 1]) + ans = max(ans, x - mi) + f[i][j] = min(x, mi) + return ans diff --git a/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.ts b/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.ts new file mode 100644 index 0000000000000..17bc7718bfb3a --- /dev/null +++ b/solution/3100-3199/3148.Maximum Difference Score in a Grid/Solution.ts @@ -0,0 +1,19 @@ +function maxScore(grid: number[][]): number { + const [m, n] = [grid.length, grid[0].length]; + const f: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => 0)); + let ans = -Infinity; + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + let mi = Infinity; + if (i) { + mi = Math.min(mi, f[i - 1][j]); + } + if (j) { + mi = Math.min(mi, f[i][j - 1]); + } + ans = Math.max(ans, grid[i][j] - mi); + f[i][j] = Math.min(mi, grid[i][j]); + } + } + return ans; +}