|
| 1 | +# Max Points You Can Obtain From Cards |
| 2 | + |
| 3 | +Given an array of integers representing card values, write a function to calculate the maximum score you can achieve by |
| 4 | +picking exactly k cards. |
| 5 | + |
| 6 | +You must pick cards in order from either end. You can take some cards from the beginning, then switch to taking cards |
| 7 | +from the end, but you cannot skip cards or pick from the middle. |
| 8 | + |
| 9 | +For example, with k = 3: |
| 10 | + |
| 11 | +- Take the first 3 cards: valid |
| 12 | +- Take the last 3 cards: valid |
| 13 | +- Take the first card, then the last 2 cards: valid |
| 14 | +- Take the first 2 cards, then the last card: valid |
| 15 | +- Take card at index 0, skip some, then take card at index 5: not valid (skipping cards) |
| 16 | + |
| 17 | +## Constraints |
| 18 | + |
| 19 | +- 1 <= k <= cards.length |
| 20 | +- 1 <= `cards.length` <= 10^5 |
| 21 | +- 1 <= `cards[i]` <= 10^4 |
| 22 | + |
| 23 | +## Examples |
| 24 | + |
| 25 | +Example 1: |
| 26 | +```text |
| 27 | +Input: cardPoints = [1,2,3,4,5,6,1], k = 3 |
| 28 | +Output: 12 |
| 29 | +Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize |
| 30 | +your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. |
| 31 | +``` |
| 32 | + |
| 33 | +Example 2: |
| 34 | +```text |
| 35 | +Input: cardPoints = [2,2,2], k = 2 |
| 36 | +Output: 4 |
| 37 | +Explanation: Regardless of which two cards you take, your score will always be 4. |
| 38 | +``` |
| 39 | + |
| 40 | +Example 3: |
| 41 | +```text |
| 42 | +Input: cardPoints = [9,7,7,9,7,7,9], k = 7 |
| 43 | +Output: 55 |
| 44 | +Explanation: You have to take all the cards. Your score is the sum of points of all cards. |
| 45 | +``` |
| 46 | + |
| 47 | +## Topics |
| 48 | + |
| 49 | +- Array |
| 50 | +- Sliding Window |
| 51 | +- Prefix Sum |
| 52 | + |
| 53 | +## Solutions |
| 54 | + |
| 55 | +When you pick k cards from either end of the array, you're actually leaving behind n - k consecutive cards in the middle |
| 56 | +that you didn't pick. |
| 57 | + |
| 58 | +For example, with cards = [2,11,4,5,3,9,2] and k = 3: |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +Every possible way to pick 3 cards from the ends corresponds to a different window of 4 cards (n - k = 7 - 3 = 4) in the |
| 63 | +middle that we're NOT picking. |
| 64 | + |
| 65 | +### Why This Matters |
| 66 | + |
| 67 | +Since we know the total sum of all cards, we can calculate: |
| 68 | + |
| 69 | +Sum of picked cards = Total sum - Sum of unpicked cards |
| 70 | + |
| 71 | +So to maximize the sum of picked cards, we need to minimize the sum of unpicked cards. |
| 72 | + |
| 73 | +This transforms the problem: instead of trying all combinations of picking from ends, we find the minimum sum of any |
| 74 | +window of size n - k. |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +### Algorithm |
| 79 | + |
| 80 | +Use a fixed-length sliding window of size n - k to find the minimum sum of any consecutive n - k cards. For each window |
| 81 | +position, calculate total - window_sum to get the corresponding score, and track the maximum. |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
0 commit comments