Skip to content

Commit 76bff6c

Browse files
committed
Leetcode | Maximum Number of Points with Cost | Kotlin
1 parent e144e16 commit 76bff6c

3 files changed

Lines changed: 109 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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.devstromo.medium.p1937
2+
3+
class SolutionKt {
4+
fun maxPoints(points: Array<IntArray>): Long {
5+
val m = points.size
6+
val n = points[0].size
7+
var prev = LongArray(n) { points[0][it].toLong() }
8+
9+
for (i in 1 until m) {
10+
val left = LongArray(n)
11+
val right = LongArray(n)
12+
val curr = LongArray(n)
13+
14+
// Build left max array
15+
left[0] = prev[0]
16+
for (j in 1 until n) {
17+
left[j] = maxOf(left[j - 1] - 1, prev[j])
18+
}
19+
20+
// Build right max array
21+
right[n - 1] = prev[n - 1]
22+
for (j in n - 2 downTo 0) {
23+
right[j] = maxOf(right[j + 1] - 1, prev[j])
24+
}
25+
26+
// Calculate current row's maximum points
27+
for (j in 0 until n) {
28+
curr[j] = points[i][j] + maxOf(left[j], right[j])
29+
}
30+
31+
prev = curr
32+
}
33+
34+
return prev.max()
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.devstromo.medium.p1937
2+
3+
import org.junit.jupiter.api.Assertions
4+
import org.junit.jupiter.api.Test
5+
6+
class SolutionKtUnitTest {
7+
private val solution = SolutionKt()
8+
9+
@Test
10+
fun `Test Maximum Number of Points with Cost`() {
11+
Assertions.assertEquals(
12+
9, solution.maxPoints(
13+
arrayOf(
14+
intArrayOf(1, 2, 3),
15+
intArrayOf(1, 5, 1),
16+
intArrayOf(3, 1, 1)
17+
)
18+
)
19+
)
20+
Assertions.assertEquals(
21+
11, solution.maxPoints(
22+
arrayOf(
23+
intArrayOf(1, 5),
24+
intArrayOf(2, 3),
25+
intArrayOf(4, 2)
26+
)
27+
)
28+
)
29+
}
30+
31+
}

0 commit comments

Comments
 (0)