Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7058289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:05:42+00:00 2026-05-28T04:05:42+00:00

This is my code: static int cardGameValue(List<int> D, int myScore, int opponentScore) { if

  • 0

This is my code:

    static int cardGameValue(List<int> D, int myScore, int opponentScore)
    {
        if (D.Count == 0) return myScore;
        else if (D.Count == 1)
        {
            opponentScore += D[0];
            return myScore;
        }
        else
        {
            if (D[0] <= D[D.Count - 1])
            {
                opponentScore += D[D.Count - 1];
                D.RemoveAt(D.Count - 1);
            }
            else
            {
                opponentScore += D[0];
                D.RemoveAt(0);
            }

            int left = cardGameValue(new List<int>(D.GetRange(1, D.Count - 1)), myScore + D[0], opponentScore);

            int right = cardGameValue(new List<int>(D.GetRange(0, D.Count - 1)), myScore + D[D.Count - 1], opponentScore);

            if (left >= right)
            {
                return left;
            }
            else
            {
                return right;
            }
        }
    }
}

My code takes a set of cards and represents your maximum possible score when playing against a deterministic opponent. After each of your opponent’s plays you have 2 choices until cards are all picked. Is there a way to somehow store my results of the iterations so I can improve my algorithm? So the recursion doesn’t do unnecessary iterations? Because after 40 or 50 cards it becomes very slow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:05:43+00:00Added an answer on May 28, 2026 at 4:05 am

    You only access the first or the last element in the list D. Rather than pass the exact list around, you can pass the full list of cards (or even better: as an int array) together with the index of the first and last position.

    It’s much faster to calculate the opponent’s score after the calculation is complete: myScore and opponentScore add up to the sum of the values of the cards, so you can do this in O(n) time. This way, you can eliminate all the code that updates opponentScore.

    You don’t need to pass myScore either. If you let cardGameValue return the best score obtained from only the remaining cards.

    Finally, if you work with the first and last index, you can store the score in a 2D array, indexed by first and last. If all the cards have positive values, then the score must be positive if there are at least two cards remaining.

    So at the beginning of the call, you check if the cached score is positive. If it is, you can return it right away. If not, you have to calculate it, and then store it in the cache array.

    This is what you end up with:

    static int cardGameValue(int[] D, int first, int last) {
        scores = new int[last + 1, last + 1];
        return cardGameValue(D, first, last, scores);
    }
    
    static int cardGameValue(int[] D, int first, int last, int[,] scores) {
        // If we have at most 1 card, our score is 0:
        if (first >= last)
            return 0;
    
        // Otherwise, get the score from the cache array. 
        // If it is positive, return the value.
        int score = scores[first, last];
        if (score > 0)
            return score;
    
        // Keep the original first and last 
        // for filling in the computed value later.
        int firstOriginal = first;
        int lastOriginal = last;
    
        // Let the opponent pick a card:
        if (D[first] <= D[last])
            last--;
        else
            first++;
    
        // Choose our best card:
        int left = D[first] + cardGameValue(D, first + 1, last, scores);
        int right = D[last] + cardGameValue(D, first, last - 1, scores);
        score = Math.Max(left, right);
    
        // and enter the score into the cache array:
        scores[firstOriginal, lastOriginal] = score;
    
        // Finally, return the computed score.
        return score;
    }
    

    Even for 300 cards, this runs in less than 1 millisecond on my machine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: static void Main(string[] args) { var persons = new List<Person> {
Today I came across this question: you have a code static int counter =
const static int foo = 42; I saw this in some code here on
I have this code public static Boolean freq[] = new Boolean[Global.iParameter[2]]; freq[Global.iParameter[2]] = false;
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
I have this code: public static int MAX; public static int MIN; public static
I try to compile this code: static uint64_t push(int fd, SOCKET sock, SSL *ssl,
i have this code: static ArrayList<Integer> output_list = new ArrayList<Integer>(); static ArrayList<Integer> pair_list =
Code: static int counter = 0; int add(int x) { counter++; return ++x; }
Definition(Core.h): static int (*foolink)(int*, char*, key*, key*); Also redefined in Core.cpp. This code causing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.