From 78d28f3411d14745eeb30258660c28b3e120918a Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 17 Aug 2024 19:58:22 +0530 Subject: [PATCH] Create 1937. Maximum Number of Points with Cost --- 1937. Maximum Number of Points with Cost | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1937. Maximum Number of Points with Cost diff --git a/1937. Maximum Number of Points with Cost b/1937. Maximum Number of Points with Cost new file mode 100644 index 0000000..23659ca --- /dev/null +++ b/1937. Maximum Number of Points with Cost @@ -0,0 +1,30 @@ +class Solution { +public: + long long maxPoints(vector>& points) { + //dynamic programming + int m = points.size(), n = points[0].size(); + long long currMax; + vector 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()); + } +};