Skip to content

Commit 3f8ff74

Browse files
committed
Leetcode | Maximum Number of Points with Cost | Java
1 parent 00abb91 commit 3f8ff74

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 1937. Maximum Number of Points with Cost
2+
3+
You are given an `m x n` integer matrix `points` (0-indexed). Starting with `0` points, you want to maximize the number of points you can get from the matrix.
4+
5+
To gain points, you must pick one cell in each row. Picking the cell at coordinates `(r, c)` will add `points[r][c]` to your score.
6+
7+
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 r and r + 1 (where `0 <= r < m - 1`), picking cells at coordinates `(r, c1)` and `(r + 1, c2)` will subtract `abs(c1 - c2)` from your score.
8+
9+
Return the maximum number of points you can achieve.
10+
11+
`abs(x)` is defined as:
12+
13+
- `x` for `x >= 0`.
14+
- `-x` for `x < 0`.
15+
16+
Example 1:
17+
18+
Input: points = [[1,2,3],[1,5,1],[3,1,1]]
19+
Output: 9
20+
Explanation:
21+
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
22+
You add 3 + 5 + 3 = 11 to your score.
23+
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
24+
Your final score is 11 - 2 = 9.
25+
26+
Example 2:
27+
28+
Input: points = [[1,5],[2,3],[4,2]]
29+
Output: 11
30+
Explanation:
31+
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
32+
You add 5 + 3 + 4 = 12 to your score.
33+
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
34+
Your final score is 12 - 1 = 11.
35+
36+
Constraints:
37+
38+
- `m == points.length`
39+
- `n == points[r].length`
40+
- `1 <= m, n <= 10^5`
41+
- `1 <= m * n <= 10^5`
42+
- `0 <= points[r][c] <= 10^5`
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.devstromo.medium.p1937;
2+
3+
public class Solution {
4+
public long maxPoints(int[][] points) {
5+
int m = points.length;
6+
int n = points[0].length;
7+
8+
long[] prev = new long[n];
9+
for (int j = 0; j < n; j++) {
10+
prev[j] = points[0][j];
11+
}
12+
13+
for (int i = 1; i < m; i++) {
14+
long[] left = new long[n];
15+
long[] right = new long[n];
16+
long[] curr = new long[n];
17+
18+
// Left to right
19+
left[0] = prev[0];
20+
for (int j = 1; j < n; j++) {
21+
left[j] = Math.max(left[j - 1] - 1, prev[j]);
22+
}
23+
24+
// Right to left
25+
right[n - 1] = prev[n - 1];
26+
for (int j = n - 2; j >= 0; j--) {
27+
right[j] = Math.max(right[j + 1] - 1, prev[j]);
28+
}
29+
30+
// Choose the best from left or right
31+
for (int j = 0; j < n; j++) {
32+
curr[j] = points[i][j] + Math.max(left[j], right[j]);
33+
}
34+
35+
prev = curr;
36+
}
37+
38+
long max = 0;
39+
for (long val : prev) {
40+
max = Math.max(max, val);
41+
}
42+
43+
return max;
44+
}
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.devstromo.medium.p1937;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class SolutionUnitTest {
9+
private final Solution solution = new Solution();
10+
11+
@Test
12+
@DisplayName("Test Maximum Number of Points with Cost")
13+
void testMaximumNumberOfPointsWithCost() {
14+
assertEquals(9, solution.maxPoints(new int[][]{
15+
{1, 2, 3},
16+
{1, 5, 1},
17+
{3, 1, 1}
18+
}));
19+
assertEquals(11, solution.maxPoints(new int[][]{
20+
{1, 5},
21+
{2, 3},
22+
{4, 2}
23+
}));
24+
}
25+
}

0 commit comments

Comments
 (0)