You are given an m x n integer matrix points (0-indexed). Starting with 0 points, you want to maximize the number of points you can get from the matrix.
To gain points, you must pick one cell in each row. Picking the cell at coordinates (r, c) will add points[r][c] to your score.
However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows r and r + 1 (where 0 <= r < m - 1), picking cells at coordinates (r, c1) and (r + 1, c2) will subtract abs(c1 - c2) from your score.
Return the maximum number of points you can achieve.
abs(x) is defined as:
x for x >= 0.
-x for x < 0.
new solution update 2022.05.13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: longlongmaxPoints(vector<vector<int>>& points){ int m = points[0].size(); vector<longlong> dp(m, 0), prv(m, 0); for(auto& r : points) { for(longlong i = 0, ma = INT_MIN; i < m; i++) { dp[i] = ma = max(ma - 1, prv[i]); } for(longlong i = m - 1, ma = INT_MIN; i >= 0; i--) { dp[i] = ma = max(ma - 1, dp[i]); dp[i] += r[i]; } swap(dp, prv); } return *max_element(begin(prv),end(prv)); } };