Maximum Points You Can Obtain from Cards

Sliding Window / 2 Pointer Constant Window Medium
  • This problem concept is utilized in many areas such as Game Development
  • In games like Hearthstone or Clash Royale, optimal strategies can be modeled using similar logic, where players have to choose cards with the most scores from a given set given constraints
  • The algorithm used to solve this problem can be used to build the AI for such competitive card games
  • So, next time you play a card game, remember that you may be battling against a software program that uses this concept to decide its moves!

Given N cards arranged in a row, each card has an associated score denoted by the cardScore array. Choose exactly k cards. In each step, a card can be chosen either from the beginning or the end of the row. The score is the sum of the scores of the chosen cards.


Return the maximum score that can be obtained.

Examples:

Input : cardScore = [1, 2, 3, 4, 5, 6] , k = 3

Output : 15

Explanation : Choosing the rightmost cards will maximize your total score. So optimal cards chosen are the rightmost three cards 4 , 5 , 6.

Th score is 4 + 5 + 6 => 15.

Input : cardScore = [5, 4, 1, 8, 7, 1, 3 ] , k = 3

Output : 12

Explanation : In first step we will choose card from beginning with score of 5.

In second step we will choose the card from beginning again with score of 4.

In third step we will choose the card from end with score of 3.

The total score is 5 + 4 + 3 => 12

Input : cardScore = [9, 10, 1, 2, 3, 5] , k = 5

Constraints

  • 1 <= cardScore.length <= 105
  • 1 <= cardScore[i] <=104
  • 1 <= k <= cardScore.length

Hints

  • "Compute the prefix sum for the first k cards from the beginning of the array. Compute the suffix sum for the last k cards from the end of the array. Combine these sums for all possible combinations (e.g., take i cards from the beginning and k−i cards from the end)."
  • Start with k cards from the beginning as the initial score. Gradually replace one card from the beginning with one card from the end, updating the score dynamically to find the maximum.

Company Tags

Salesforce Byju's Dropbox JPMorgan Chase Rakuten HashiCorp NVIDIA Target Walmart OYO Rooms Databricks Alibaba Swiggy MongoDB Cloudflare Deloitte Visa Docker Boston Consulting Group Lyft Morgan Stanley Zoho Micron Technology Mastercard Instacart Google Microsoft Amazon Meta Apple Netflix Adobe