# Solution For Maximum Number Of Points With Cost

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

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

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).

## Step by Step Implementation For Maximum Number Of Points With Cost

/** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Point(int a, int b) { x = a; y = b; } * } */ public class Solution { public int maxPoints(Point[] points) { if (points == null || points.length == 0) return 0; if (points.length == 1) return 1; // key: slope, value: number of points with that slope Mapmap = new HashMap<>(); int result = 0; for (int i = 0; i < points.length; i++) { map.clear(); int duplicate = 1; for (int j = i + 1; j < points.length; j++) { if (points[i].x == points[j].x && points[i].y == points[j].y) { duplicate++; continue; } double slope = points[i].x == points[j].x ? Double.MAX_VALUE : 0.0 + (double)(points[i].y - points[j].y) / (double)(points[i].x - points[j].x); if (!map.containsKey(slope)) { map.put(slope, 1); } else { map.put(slope, map.get(slope) + 1); } } // update result for (Integer count : map.values()) { if (count + duplicate > result) { result = count + duplicate; } } // vertical line case if (map.containsKey(Double.MAX_VALUE)) { if (map.get(Double.MAX_VALUE) + duplicate > result) { result = map.get(Double.MAX_VALUE) + duplicate; } } } return result; } }

def maxPoints(costs): # Sort the costs in increasing order of cost costs.sort(key = lambda x: x[0]) # Initialize the dp array dp = [0 for i in range(len(costs))] # Base case dp[0] = costs[0][1] # Fill dp in bottom up manner for i in range(1, len(costs)): # Take the maximum of the two options # 1. Don't include the ith activity # 2. Include the ith activity dp[i] = max(dp[i-1], dp[i-1] + costs[i][1]) # The maximum cost will be the last # element in the dp array return dp[-1]

var maximumNumberOfPoints = function(cost) { // Sort the cost array in ascending order cost.sort((a, b) => a - b); // Initialize maxPoints to 0 let maxPoints = 0; // Iterate through the cost array for (let i = 0; i < cost.length; i++) { // Add the current cost to maxPoints maxPoints += cost[i]; // Subtract the current cost from the next cost cost[i + 1] -= cost[i]; } // Return maxPoints return maxPoints; };

// Maximum Number of Points with Cost // Given an array of points and an integer K, find the maximum number of points that can be taken such that the sum of the cost of the points taken is <= K. #include#include #include using namespace std; struct Point { int x; int y; int cost; }; bool sortByCost(Point a, Point b) { return a.cost < b.cost; } int maximumNumberOfPoints(vector points, int K) { // sort the points by cost sort(points.begin(), points.end(), sortByCost); // keep track of the maximum number of points that can be taken int maxPoints = 0; // keep track of the current sum of costs int currCost = 0; // iterate through the points for (int i = 0; i < points.size(); i++) { // add the cost of the current point to the sum currCost += points[i].cost; // if the sum is less than or equal to K, update the maximum number of points if (currCost <= K) { maxPoints++; } // if the sum is greater than K, stop iterating else { break; } } return maxPoints; } int main() { // create test case vector points = {{1, 2, 3}, {2, 4, 6}, {3, 6, 9}}; int K = 10; // output should be 3 cout << maximumNumberOfPoints(points, K) << endl; return 0; }

int maximumNumberOfPoints(vector& cost, int target) { int n = cost.size(); vector dp(target+1); for(int i=0;i =cost[i];j--) { dp[j] = max(dp[j],dp[j-cost[i]]+1); } } return dp[target]; }