Skip to content

Commit

Permalink
Create 1937. Maximum Number of Points with Cost (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Aug 17, 2024
2 parents aa9fb0a + 78d28f3 commit 50ebeff
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 1937. Maximum Number of Points with Cost
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
long long maxPoints(vector<vector<int>>& points) {
//dynamic programming
int m = points.size(), n = points[0].size();
long long currMax;
vector<long long> maxPoints(n), rightRow(n);
for(auto row: points){
currMax = 0;

//Calculate maximum points from the right
//We do this as we need to change the value of maxPoints[j]
for(int j = n-1; j >= 0; j--){
currMax = max(currMax, maxPoints[j]);
rightRow[j] = currMax--;
}

currMax = 0; //Maximum points from the left
for(int j = 0; j < n; j++){
currMax = max(currMax, maxPoints[j]);
//Consider maximum points possible if we pick
//the cell of index j of current row
maxPoints[j] = max(currMax--, rightRow[j]) + row[j];
}
}

// return maximum possible amount of points
return *max_element(maxPoints.begin(), maxPoints.end());
}
};

0 comments on commit 50ebeff

Please sign in to comment.