File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ long long maxPoints(vector<vector<int>>& points) {
4
+ //dynamic programming
5
+ int m = points.size(), n = points[0].size();
6
+ long long currMax;
7
+ vector<long long> maxPoints(n), rightRow(n);
8
+ for(auto row: points){
9
+ currMax = 0;
10
+
11
+ //Calculate maximum points from the right
12
+ //We do this as we need to change the value of maxPoints[j]
13
+ for(int j = n-1; j >= 0; j--){
14
+ currMax = max(currMax, maxPoints[j]);
15
+ rightRow[j] = currMax--;
16
+ }
17
+
18
+ currMax = 0; //Maximum points from the left
19
+ for(int j = 0; j < n; j++){
20
+ currMax = max(currMax, maxPoints[j]);
21
+ //Consider maximum points possible if we pick
22
+ //the cell of index j of current row
23
+ maxPoints[j] = max(currMax--, rightRow[j]) + row[j];
24
+ }
25
+ }
26
+
27
+ // return maximum possible amount of points
28
+ return *max_element(maxPoints.begin(), maxPoints.end());
29
+ }
30
+ };
You can’t perform that action at this time.
0 commit comments