From 55209a495a5a1b768b78c10f39a1a00d29a31376 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 17 Aug 2024 22:55:57 +0200 Subject: [PATCH] =?UTF-8?q?java:=20add=20=C2=AB1937.=20Maximum=20Number=20?= =?UTF-8?q?of=20Points=20with=20Cost=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- java/maximum-number-of-points-with-cost.java | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 java/maximum-number-of-points-with-cost.java diff --git a/java/maximum-number-of-points-with-cost.java b/java/maximum-number-of-points-with-cost.java new file mode 100644 index 0000000..e3d120f --- /dev/null +++ b/java/maximum-number-of-points-with-cost.java @@ -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; + } +}