Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 1937. Maximum Number of Points with Cost #559

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
};
Loading