## Similar Problems

Similar Problems not available

# Maximum Number Of Points With Cost - Leetcode Solution

## Companies:

LeetCode: Maximum Number Of Points With Cost Leetcode Solution

Difficulty: Medium

Topics: dynamic-programming array

The problem statement for the Maximum Number Of Points With Cost problem on LeetCode is as follows:

You are given a rectangular matrix of integers matrix and three integers rMove, cMove, and K. You can perform at most K moves in total and move in one of the four directions: up, down, left, and right. You can only make a move if the destination cell is within the matrix and has a value not larger than a threshold.

You start at cell (0, 0) and want to reach the last cell (n-1, m-1). A point is scored according to the value of the cell at the destination. You need to maximize the points scored.

Return the maximum number of points you can score.

A detailed solution to this problem follows:

Approach:

We can use a dynamic programming approach to solve this problem. Let dp[i][j][k] be the maximum number of points we can achieve starting from the cell (i, j) after k moves. Every move can be in one of the four possible directions: up, down, left, or right.

We can compute dp[i][j][k] by considering all the possible moves we can make from cell (i, j) and by selecting the move that gives us the maximum number of points. When computing dp[i][j][k], we must be careful not to exceed the maximum number of moves K.

The maximum number of moves we can make from a cell (i, j) is equal to the minimum of the distance of this cell to the boundary of the matrix in the vertical and horizontal directions. This is because, after we have made this number of moves, we will have reached either the top, bottom, left, or right boundary of the matrix.

To compute dp[i][j][k], we iterate over all the possible directions of the move, and we compute the number of points we can achieve if we make that move. We select the move that gives us the maximum number of points. If k == 0, we return 0, as we cannot make any more moves.

Finally, we return dp[0][0][K], which is the maximum number of points we can achieve starting from cell (0, 0) after K moves.

Code:

The code for the above approach is given below. In the code, we use -1 to represent cells that are outside the matrix or have a value larger than the threshold. (We use the variable INF to represent the threshold).

```
int dp[50][50][55];
int solve(vector<vector<int>>& mat, int i, int j, int k, int n, int m, int INF) {
if (i < 0 || i >= n || j < 0 || j >= m || mat[i][j] > INF)
return -1;
if (i == n-1 && j == m-1)
return mat[i][j];
if (k == 0)
return 0;
if (dp[i][j][k] != -1)
return dp[i][j][k];
int mx = 0;
for (int di = -1; di <= 1; ++di) {
for (int dj = -1; dj <= 1; ++dj) {
if (di == 0 && dj == 0) continue;
int ni = i + di, nj = j + dj;
int cost = solve(mat, ni, nj, k-1, n, m, INF);
mx = max(mx, cost);
}
}
dp[i][j][k] = (mx == -1) ? -1 : mx + mat[i][j];
return dp[i][j][k];
}
int maxPoints(vector<vector<int>>& mat, int rMove, int cMove, int K) {
int n = mat.size(), m = mat[0].size();
int INF = mat[0][0];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
INF = max(INF, mat[i][j]);
dp[i][j][0] = -1;
for (int k = 1; k <= K; ++k)
dp[i][j][k] = -1;
}
}
INF = min(INF, 50*50);
return solve(mat, 0, 0, K, n, m, INF);
}
```

Time Complexity:

The time complexity of the above approach is O(N^4K), where N is the maximum of the number of rows and columns in the matrix. This is because for each cell (i, j) in the matrix, we compute the maximum number of points we can achieve after K moves. For each cell, we consider all the possible moves we can make in the four possible directions and check if the new cell is within the matrix and has a value not larger than the threshold. We perform this computation for K moves. Therefore, the time complexity is O(N^4K).

## Maximum Number Of Points With Cost Solution Code

`1`