# Solution For Maximum Points You Can Obtain From Cards

Maximum Points You Can Obtain From Cards is a problem on LeetCode that asks you to find the maximum number of points that you can obtain from selecting a given number of cards from an array of cards, with the constraint that you can only select from the beginning or end of the array.

To solve this problem, we can use dynamic programming. We can create two arrays, left and right, where left[i] represents the maximum points that can be obtained from selecting i cards from the beginning of the array, and right[i] represents the maximum points that can be obtained from selecting i cards from the end of the array.

First, we can fill the left array by taking the first card and adding it to the sum of the maximum points that can be obtained from selecting i-1 cards from the beginning of the array. We can use a for loop to iterate through the array from 1 to k, where k is the number of cards we want to select.

Next, we can fill the right array in a similar manner, by taking the last card and adding it to the sum of the maximum points that can be obtained from selecting i-1 cards from the end of the array. We can use a for loop to iterate through the array from n-1 to n-k-1, where n is the length of the array and k is the number of cards we want to select.

Finally, we can iterate through the left and right arrays to find the maximum sum of points that can be obtained by selecting cards from both sides of the array. We can use a for loop to iterate through the array from 0 to k, where k is the number of cards we want to select, and add the maximum points from selecting i cards from the left array and k-i cards from the right array.

Here’s the Python code:

“`
def maxScore(cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
left =  * (k+1)
right =  * (k+1)

``````for i in range(1, k+1):
left[i] = left[i-1] + cardPoints[i-1]

for i in range(n-1, n-k-1, -1):
right[n-i] = right[n-i-1] + cardPoints[i]

max_score = 0
for i in range(k+1):
max_score = max(max_score, left[i] + right[k-i])

return max_score
``````

“`

The time complexity of this solution is O(k), since we iterate through the array twice and then iterate through the left and right arrays once. The space complexity is O(k), since we use two arrays of size k+1.

## Step by Step Implementation For Maximum Points You Can Obtain From Cards

```There are several ways to solve this problem. One approach would be to use a greedy algorithm, where you always choose the card with the maximum points. However, this does not always give the optimal solution.

Another approach would be to use dynamic programming. We can create an array dp, where dp[i] represents the maximum points we can obtain from the first i cards. Then, we can update this array using the following recurrence:

dp[i] = max(dp[i-1], dp[i-2] + points[i])

The base cases would be dp = points and dp = max(points, points).

We can then return dp[n-1], where n is the number of cards.```
```There are several ways to approach this problem. One way would be to keep track of the maximum points you can obtain from each card, and then return the maximum of those values.

Another way would be to keep track of the maximum points you can obtain from each card, and then return the maximum of those values.```
```There are several ways to approach this problem. One way would be to keep track of the maximum sum of points that can be obtained from each card, and then return the maximum of those sums.

Another way would be to keep track of the maximum sum of points that can be obtained from each card, and then return the maximum of those sums.```
```There are several ways to approach this problem. One way would be to keep track of the maximum points possible at each card value, and then return the maximum of those values.

Another way would be to keep track of the maximum points possible at each card value, and then return the maximum of those values.

Yet another way would be to keep track of both the maximum and minimum points possible at each card value, and then return the maximum of those values.

Whichever approach you take, the basic idea is the same: keep track of the maximum points possible at each card value, and then return the maximum of those values.```
```There are several ways to solve this problem, but one approach would be as follows:

1. Create a list to store the points for each card.

2. Iterate through the list of cards, adding the points for each card to the list.

3. Sort the list in descending order.

4. Return the first element in the list (this will be the maximum points you can obtain).```
Scroll to Top

## Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]