The Indian Engineer

Problem 1937 Maximum Number of Points with Cost

Posted on 7 mins

Array Dynamic-Programming Matrix

Problem Statement

Link - Problem 1937

Question

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:

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:

Solution

class Solution {
public:
    long long maxPoints(vector<vector<int>>& points) {
        vector<vector<long long>> dp(points.size(), vector<long long>(points[0].size(), -1));

        for (int i = 0; i < points[0].size(); ++i) {
            dp[0][i] = points[0][i];
        }

        for (int i = 1; i < points.size(); ++i) {
            vector<long long> left_dp(points[i].size(), -1);
            vector<long long> right_dp(points[i].size(), -1);

            left_dp[0] = dp[i - 1][0];
            for (int k = 1; k < points[i].size(); ++k) {
                left_dp[k] = max(left_dp[k - 1], dp[i - 1][k] + k);
            }

            right_dp.back() = dp[i - 1].back() - points[i].size() + 1;
            for (int k = points[i].size() - 2; k >= 0; --k) {
                right_dp[k] = max(right_dp[k + 1], dp[i - 1][k] - k);
            }

            for (int j = 0; j < points[i].size(); ++j) {
                dp[i][j] = max(left_dp[j] - j, right_dp[j] + j) + points[i][j];
            }
        }

        long long max_ans = -1;
        for (const auto v : dp.back()) {
            max_ans = max(max_ans, v);
        }

        return max_ans;
    }
};

Complexity Analysis

| Algorithm           | Time Complexity | Space Complexity |
| ------------------- | --------------- | ---------------- |
| Dynamic Programming | O(nm)           | O(nm)            |

Explanation

Intial Thoughts

To approach this problem, first consider the constraints and the goal. We need to maximize the points gained from the matrix by picking one cell in each row. The points gained from each cell are added to the score, but we lose points if the picked cells are too far apart in adjacent rows. We should start by analyzing how the points are distributed in the matrix and think about strategies to minimize the loss of points due to distance. The problem can be broken down into smaller sub-problems, and we should look for patterns or relationships between rows to find an optimal solution.

Intuitive Analysis

Intuitively, we can think of this problem as trying to find a path through the matrix that maximizes the points gained. We should consider the trade-off between gaining points from a cell and losing points due to its distance from the previous cell. A greedy approach might not work because we need to consider the long-term effects of our choices. Instead, we can use dynamic programming to build up a solution by considering the optimal choices for each row based on the previous row. By analyzing the problem in this way, we can develop a strategy to find the maximum points that can be achieved.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Try using dynamic programming.

dp[i][j] is the maximum number of points you can have if points[i][j] is the most recent cell you picked.


Similar Questions:

Title URL Difficulty
Minimum Path Sum https://leetcode.com/problems/minimum-path-sum Medium
Minimize the Difference Between Target and Chosen Elements https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements Medium