Leetcode 1937: Maximum Number of Points with Cost

grid47
grid47
Exploring patterns and algorithms
Apr 27, 2024 8 min read

You are given a matrix of integers points with dimensions m x n (0-indexed). Initially, you are at score 0, and your goal is to maximize the score by selecting one cell in each row. To gain points, you can select a cell (r, c) from each row. The value at points[r][c] will add to your score. However, if you choose cells from different columns in adjacent rows, the difference between their column indices will subtract from your score. Specifically, if you select a cell at (r, c1) in row r and a cell at (r + 1, c2) in row r + 1, the penalty is abs(c1 - c2). Your task is to return the maximum score you can achieve by choosing cells from each row.
Problem
Approach
Steps
Complexity
Input: The input consists of an integer matrix `points` of size `m x n`.
Example: points = [[3, 1, 4], [1, 2, 5], [2, 6, 7]]
Constraints:
• 1 <= m, n <= 10^5
• 1 <= m * n <= 10^5
• 0 <= points[r][c] <= 10^5
Output: The output is the maximum score you can achieve by selecting cells from each row.
Example: Output: 15
Constraints:
• The output is an integer.
Goal: Maximize the score by selecting cells in each row while minimizing the penalty caused by column differences in adjacent rows.
Steps:
• Start from the first row and select the cell with the maximum value.
• For each subsequent row, select the cell that maximizes the score while minimizing the column difference penalty with the cell selected in the previous row.
Goal: The matrix can have up to 100,000 rows and columns.
Steps:
• Matrix size must be at most 10^5.
Assumptions:
• The matrix is non-empty and contains only non-negative integers.
Input: Input: [[3, 1, 4], [1, 2, 5], [2, 6, 7]]
Explanation: Select the cell at (0, 2), (1, 2), and (2, 1) for the optimal score of 15. The penalty for column difference is 1.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus