Maximum Number of Points with Cost - Problem

Imagine you're a treasure hunter navigating through an ancient temple with m floors and n rooms per floor. Each room contains a treasure worth points[r][c] points. Your goal is to maximize your treasure collection while following these rules:

  • Must visit exactly one room per floor - you can't skip floors or visit multiple rooms on the same floor
  • Moving costs energy - if you pick room c1 on floor r and room c2 on floor r+1, you lose |c1 - c2| points due to the energy spent traveling horizontally

Given an m × n matrix where points[i][j] represents the treasure value in room j on floor i, return the maximum points you can collect on your journey from the top floor to the bottom floor.

Think strategically! Sometimes it's worth picking a room with fewer points if it positions you better for the floors below.

Input & Output

example_1.py — Basic Matrix
$ Input: points = [[1,2,3],[1,5,1],[3,1,1]]
Output: 9
💡 Note: Optimal path: (0,1) → (1,1) → (2,0) = 2 + 5 + 3 - |1-1| - |1-0| = 10 - 0 - 1 = 9
example_2.py — Single Column
$ Input: points = [[1,5],[2,3],[4,2]]
Output: 11
💡 Note: Optimal path: (0,1) → (1,0) → (2,0) = 5 + 2 + 4 - |1-0| - |0-0| = 11 - 1 - 0 = 10. Wait, let's recalculate: (0,1) → (1,1) → (2,0) = 5 + 3 + 4 - 0 - 1 = 11
example_3.py — Large Penalties
$ Input: points = [[1,2,3],[1,5,1],[3,1,1]]
Output: 9
💡 Note: Even with potential high movement costs, the algorithm finds the optimal balance between high-value cells and movement penalties

Constraints

  • m == points.length
  • n == points[r].length
  • 1 ≤ m, n ≤ 105
  • 1 ≤ m × n ≤ 105
  • 0 ≤ points[r][c] ≤ 105

Visualization

Tap to expand
Maximum Number of Points with Cost INPUT Temple with 3 floors, 3 rooms each Floor 0: 1 2 3 Floor 1: 1 5 1 Floor 2: 3 1 1 Input Matrix: points = [[1,2,3], [1,5,1],[3,1,1]] Cost = |c1 - c2| for moving between columns = Optimal path rooms ALGORITHM STEPS 1 Initialize DP dp[0] = points[0] = [1,2,3] 2 Left-to-Right Pass Compute max from left: left[j] = max(left[j-1]-1, dp[j]) 3 Right-to-Left Pass Compute max from right: right[j] = max(right[j+1]-1, dp[j]) 4 Update DP dp[j] = points[i][j] + max(left[j], right[j]) DP Transitions: Row 0: [1, 2, 3] Row 1: [2, 7, 4] (1+1, 5+2, 1+3) Row 2: [9, 7, 6] (3+6, 1+6, 1+5) FINAL RESULT Optimal Path Through Temple 3 F0, C2: 5 F1, C1: 3 F2, C0: Score Calculation: Points: 3 + 5 + 3 = 11 Cost: |2-1| + |1-0| = 2 Total: 11 - 2 = 9 Output: 9 Key Insight: Use left and right sweeps to efficiently compute the maximum value reachable from any position. This reduces O(n^2) transitions to O(n) per row, achieving O(m*n) time complexity. The trick: propagate max values while subtracting 1 for each step moved horizontally. TutorialsPoint - Maximum Number of Points with Cost | DP Approach
Asked in
Google 42 Amazon 38 Meta 28 Microsoft 25
89.2K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen