Can you point me what's wrong in my logic?

  • 0

    I could not figure out what's wrong here. Submission result says "49 / 54 test cases passed." Failed for input (10,40). Can anyone find me any clue here? Thanks!

    public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
            if (desiredTotal == 0) return true;
            if ((1 + maxChoosableInteger) * maxChoosableInteger / 2 < desiredTotal)
                return false;
            boolean[][] dp = new boolean[1+desiredTotal][1+maxChoosableInteger];        
            for(int i=1;i<=desiredTotal;i++){
                for(int j=1;j<=maxChoosableInteger;j++){                
                    if(i<=j) dp[i][j] = true; // <-- always first player wins
                        /* check if 1st player wins by its any of j number of contributions */
                        for(int k=1;k<=j;k++){
                            dp[i][j] |= !dp[i-k][j];
            return dp[desiredTotal][maxChoosableInteger];

  • 0

    Are you accounting for the fact that the numbers can only be used once?

Log in to reply

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