3148. Maximum Difference Score in a Grid Medium
1/**
2 * [3148] Maximum Difference Score in a Grid
3 *
4 * You are given an m x n matrix grid consisting of positive integers. You can move from a cell in the matrix to any other cell that is either to the bottom or to the right (not necessarily adjacent). The score of a move from a cell with the value c1 to a cell with the value c2 is c2 - c1.<!-- notionvc: 8819ca04-8606-4ecf-815b-fb77bc63b851 -->
5 * You can start at any cell, and you have to make at least one move.
6 * Return the maximum total score you can achieve.
7 *
8 * <strong class="example">Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2024/03/14/grid1.png" style="width: 240px; height: 240px;" />
10 * <div class="example-block">
11 * Input: <span class="example-io">grid = [[9,5,7,3],[8,9,6,1],[6,7,14,3],[2,5,3,1]]</span>
12 * Output: <span class="example-io">9</span>
13 * Explanation: We start at the cell (0, 1), and we perform the following moves:<br />
14 * - Move from the cell (0, 1) to (2, 1) with a score of 7 - 5 = 2.<br />
15 * - Move from the cell (2, 1) to (2, 2) with a score of 14 - 7 = 7.<br />
16 * The total score is 2 + 7 = 9.
17 * </div>
18 * <strong class="example">Example 2:
19 * <img alt="" src="https://assets.leetcode.com/uploads/2024/04/08/moregridsdrawio-1.png" style="width: 180px; height: 116px;" />
20 * <div class="example-block">
21 * Input: <span class="example-io">grid = [[4,3,2],[3,2,1]]</span>
22 * Output: <span class="example-io">-1</span>
23 * Explanation: We start at the cell (0, 0), and we perform one move: (0, 0) to (0, 1). The score is 3 - 4 = -1.
24 * </div>
25 *
26 * Constraints:
27 *
28 * m == grid.length
29 * n == grid[i].length
30 * 2 <= m, n <= 1000
31 * 4 <= m * n <= 10^5
32 * 1 <= grid[i][j] <= 10^5
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/maximum-difference-score-in-a-grid/
38// discuss: https://leetcode.com/problems/maximum-difference-score-in-a-grid/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn max_score(grid: Vec<Vec<i32>>) -> i32 {
44 0
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_3148() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.