Sponsored
Sponsored
This approach uses dynamic programming where we keep track of the maximum points obtainable for each cell using two linear passes per row: one from left to right and the other from right to left. This ensures that we account for the cost of moving between rows efficiently.
Time Complexity: O(m * n) as we iterate through the cells twice per row. Space Complexity: O(n) due to the auxiliary arrays used.
1using System;
2
3public class Solution {
4 public static int MaxPoints(int[][] points) {
5 int m = points.Length;
6 int n = points[0].Length;
7
8 int[] prevRow = new int[n];
9 Array.Copy(points[0], prevRow, n);
10
11 for (int i = 1; i < m; i++) {
12 int[] leftToRight = new int[n];
13 Array.Copy(prevRow, leftToRight, n);
14
15 for (int j = 1; j < n; j++) {
16 leftToRight[j] = Math.Max(leftToRight[j], leftToRight[j - 1] - 1);
17 }
18
19 int[] rightToLeft = new int[n];
20 Array.Copy(prevRow, rightToLeft, n);
21
22 for (int j = n - 2; j >= 0; j--) {
23 rightToLeft[j] = Math.Max(rightToLeft[j], rightToLeft[j + 1] - 1);
24 }
25
26 for (int j = 0; j < n; j++) {
27 leftToRight[j] = points[i][j] + Math.Max(leftToRight[j], rightToLeft[j]);
28 }
29
30 prevRow = leftToRight;
31 }
32
33 int maxPoints = 0;
34 foreach (var val in prevRow) {
35 if (val > maxPoints) maxPoints = val;
36 }
37
38 return maxPoints;
39 }
40
41 public static void Main() {
42 int[][] points = new int[][] {
43 new int[] {1, 2, 3},
44 new int[] {1, 5, 1},
45 new int[] {3, 1, 1}
46 };
47 Console.WriteLine("Maximum points: " + MaxPoints(points));
48 }
49}
50
This C# implementation follows a similar pattern of adjusting prevRow
with leftToRight
and rightToLeft
to calculate the maximum points efficaciously. Iterative with a focus on row-wise linear computation.
This approach uses a dynamic array to store optimal points value and computes it via a prefix and suffix maximum accumulations, thereby reducing the transition complexity while still making sure every cell's deduction is feasible within two linear passes.
Time Complexity: O(m * n). Space Complexity: O(n), reducing allocation per row iteration.
1#include <algorithm>
using namespace std;
int maxPoints(vector<vector<int>>& points) {
int m = points.size();
int n = points[0].size();
vector<int> prevRow(points[0].begin(), points[0].end());
for (int i = 1; i < m; ++i) {
vector<int> leftMax(n);
vector<int> rightMax(n);
vector<int> currentRow(n);
leftMax[0] = prevRow[0];
for (int j = 1; j < n; ++j) {
leftMax[j] = max(leftMax[j - 1] - 1, prevRow[j]);
}
rightMax[n - 1] = prevRow[n - 1];
for (int j = n - 2; j >= 0; --j) {
rightMax[j] = max(rightMax[j + 1] - 1, prevRow[j]);
}
for (int j = 0; j < n; ++j) {
currentRow[j] = points[i][j] + max(leftMax[j], rightMax[j]);
}
prevRow = currentRow;
}
return *max_element(prevRow.begin(), prevRow.end());
}
int main() {
vector<vector<int>> points = {{1, 2, 3}, {1, 5, 1}, {3, 1, 1}};
int result = maxPoints(points);
printf("Maximum points: %d\n", result);
return 0;
}
The primary aim in this C++ code is to optimize the array management and expedite the element-wise summation for determining ideal points. By using dynamic arrays, computation resources are conserved and enhanced.