Skip to content

Commit 50ebeff

Browse files
authored
Create 1937. Maximum Number of Points with Cost (#559)
2 parents aa9fb0a + 78d28f3 commit 50ebeff

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
};

0 commit comments

Comments
 (0)