-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1937_Maximum_Number_of_Points_with_Cost.cpp
More file actions
67 lines (60 loc) · 2.43 KB
/
1937_Maximum_Number_of_Points_with_Cost.cpp
File metadata and controls
67 lines (60 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
1937. Maximum Number of Points with Cost
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 themaximum number of points you can achieve.
abs(x) is defined as:
x for x >= 0.
-x for x < 0.
Example 1:
Input: points = [[1,2,3],[1,5,1],[3,1,1]]
Output: 9
Explanation:
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
Example 2:
Input: points = [[1,5],[2,3],[4,2]]
Output: 11
Explanation:
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
Constraints:
m == points.length
n == points[r].length
1 <= m, n <= 105
1 <= m * n <= 105
0 <= points[r][c] <= 105
*/
class Solution {
public:
long long maxPoints(vector<vector<int>>& points) {
int row = points.size();
int col = points[0].size();
vector<long long> dp(col, 0);
for(int i = 0; i < col; i++) {
dp[i] = points[0][i];
}
for(int r = 1; r < row; r++) {
vector<long long> leftMax(col, 0), rightMax(col, 0);
vector<long long> newDp(col, 0);
leftMax[0] = dp[0];
for(int i = 1; i < col; i++) {
leftMax[i] = max(leftMax[i-1], dp[i] + i);
}
rightMax[col-1] = dp[col-1] - (col-1);
for(int i = col-2; i >= 0; i--) {
rightMax[i] = max(rightMax[i+1], dp[i] - i);
}
for(int i = 0; i < col; i++) {
newDp[i] = max(leftMax[i] - i, rightMax[i] + i) + points[r][i];
}
dp = newDp;
}
return *max_element(dp.begin(), dp.end());
}
};