| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.MaximumNumberOfPointsWithCost; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MaximumNumberOfPointsWithCostDynamicProgramming : IMaximumNumberOfPointsWithCost |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m * n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="points"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public long MaxPoints(int[][] points) |
| 7 | 24 | | { |
| 7 | 25 | | var scores = new long[points[0].Length]; |
| | 26 | |
|
| 68 | 27 | | for (var j = 0; j < points[0].Length; j++) |
| 27 | 28 | | { |
| 27 | 29 | | scores[j] = points[0][j]; |
| 27 | 30 | | } |
| | 31 | |
|
| 42 | 32 | | for (var i = 1; i < points.Length; i++) |
| 14 | 33 | | { |
| 14 | 34 | | var leftMax = new long[points[i].Length]; |
| 14 | 35 | | var rightMax = new long[points[i].Length]; |
| | 36 | |
|
| 14 | 37 | | leftMax[0] = scores[0]; |
| | 38 | |
|
| 136 | 39 | | for (var j = 1; j < points[i].Length; j++) |
| 54 | 40 | | { |
| 54 | 41 | | leftMax[j] = Math.Max(leftMax[j - 1], scores[j] + j); |
| 54 | 42 | | } |
| | 43 | |
|
| 14 | 44 | | rightMax[points[i].Length - 1] = scores[points[i].Length - 1] - (points[i].Length - 1); |
| | 45 | |
|
| 136 | 46 | | for (var j = points[i].Length - 2; j >= 0; j--) |
| 54 | 47 | | { |
| 54 | 48 | | rightMax[j] = Math.Max(rightMax[j + 1], scores[j] - j); |
| 54 | 49 | | } |
| | 50 | |
|
| 164 | 51 | | for (var j = 0; j < points[i].Length; j++) |
| 68 | 52 | | { |
| 68 | 53 | | scores[j] = points[i][j] + Math.Max(leftMax[j] - j, rightMax[j] + j); |
| 68 | 54 | | } |
| 14 | 55 | | } |
| | 56 | |
|
| 7 | 57 | | return scores.Max(); |
| 7 | 58 | | } |
| | 59 | | } |