what is wrong with my solution????.passing 42 out of 52 testcases


  • 0
    S

    class Solution {
    public:
    bool canIWin(int maxChoosableInteger, int desiredTotal) {
    vector<int>a(25);

        int m[300];
        
        if(desiredTotal==0)
        return true;
    
        memset(m,0,sizeof(m));
        
        for(int i=1;i<=maxChoosableInteger;i++)
            a[i]=i;
        
        for(int i=1;i<=desiredTotal;i++)
        {
            for(int j=1;j<=maxChoosableInteger;j++)
            {
                if(i-a[j]>=0)
                {
                    if(m[i-a[j]]==0)
                    {
                    m[i]=1;
                  //  a[j]=-1;
                   // a.erase(remove(a.begin(),a.end(),a[j]),a.end());
                   
                    break;
                    }
                }
            }
        }
      
    if(m[desiredTotal]==1)
    return 1;
    return 0;
    }
    

    };


  • 0
    B

    I have the same idea with you at the very beginning.

    But this is not gonna work. Because we never check if duplicate numbers are being used.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.