-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1937. Maximum Number of Points with Cost (#559)
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}; |