java: add «1937. Maximum Number of Points with Cost»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-17 22:55:57 +02:00
parent 83202647ef
commit 55209a495a
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,30 @@
class Solution {
public long maxPoints(int[][] points) {
int width = points[0].length;
var previous = new long[width];
for (var row : points) {
var current = new long[width];
long runningMaximum = 0;
for (int x = 0; x < width; ++x) {
runningMaximum = Math.max(runningMaximum - 1, previous[x]);
current[x] = runningMaximum;
}
runningMaximum = 0;
for (int x = width - 1; x >= 0; --x) {
runningMaximum = Math.max(runningMaximum - 1, previous[x]);
current[x] = Math.max(current[x], runningMaximum) + row[x];
}
previous = current;
}
var answer = previous[0];
for (var candidate : previous) {
answer = Math.max(answer, candidate);
}
return answer;
}
}