1012. Points With Penalty
An arcade has m shelves, each holding n prize slots. points[r][c] is the score of the prize at row r, column c.
A player must grab exactly one prize from every shelf. Walking sideways costs effort: between two consecutive shelves, if the player took columns a and b, one point is lost for every column of distance, i.e. |a - b| points.
Return the largest possible total of picked scores minus all walking penalties, as a long. Beat the naive O(m * n^2) transition: the intended solution runs in O(m * n) time and O(n) extra space.
Example 1
- Input:
- points = [[6,1,8],[2,9,3],[7,4,5]]
- Output:
- 22
- Explanation:
Choosing columns 2, 1, 0 earns 8 + 9 + 7 = 24 and pays 1 + 1 = 2 in penalties, for 22; no other route does better.
Example 2
- Input:
- points = [[10,0,0,0],[0,0,0,12]]
- Output:
- 19
- Explanation:
Taking both big cells earns 22 but the columns are 3 apart, costing 3, so 19 beats staying in one column (12).
Constraints
1 ≤ m, n ≤ 105 and m * n ≤ 3 * 105
0 ≤ points[r][c] ≤ 105
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(m*n)
- Space
- O(n)