|
| 1 | +<h2><a href="https://leetcode.com/problems/maximum-number-of-points-with-cost">1937. Maximum Number of Points with Cost</a></h2><h3>Medium</h3><hr><p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> |
| 2 | + |
| 3 | +<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> |
| 4 | + |
| 5 | +<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> |
| 6 | + |
| 7 | +<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> |
| 8 | + |
| 9 | +<p><code>abs(x)</code> is defined as:</p> |
| 10 | + |
| 11 | +<ul> |
| 12 | + <li><code>x</code> for <code>x >= 0</code>.</li> |
| 13 | + <li><code>-x</code> for <code>x < 0</code>.</li> |
| 14 | +</ul> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong class="example">Example 1:</strong><strong> </strong></p> |
| 18 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021年07月12日-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> |
| 19 | +<pre> |
| 20 | +<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] |
| 21 | +<strong>Output:</strong> 9 |
| 22 | +<strong>Explanation:</strong> |
| 23 | +The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). |
| 24 | +You add 3 + 5 + 3 = 11 to your score. |
| 25 | +However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. |
| 26 | +Your final score is 11 - 2 = 9. |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p><strong class="example">Example 2:</strong></p> |
| 30 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021年07月12日-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> |
| 31 | +<pre> |
| 32 | +<strong>Input:</strong> points = [[1,5],[2,3],[4,2]] |
| 33 | +<strong>Output:</strong> 11 |
| 34 | +<strong>Explanation:</strong> |
| 35 | +The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). |
| 36 | +You add 5 + 3 + 4 = 12 to your score. |
| 37 | +However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. |
| 38 | +Your final score is 12 - 1 = 11. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<p><strong>Constraints:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li><code>m == points.length</code></li> |
| 46 | + <li><code>n == points[r].length</code></li> |
| 47 | + <li><code>1 <= m, n <= 10<sup>5</sup></code></li> |
| 48 | + <li><code>1 <= m * n <= 10<sup>5</sup></code></li> |
| 49 | + <li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li> |
| 50 | +</ul> |
0 commit comments