Why TLE?Please tell me!


  • 0
    D
    class Solution {
    public:
    bool canWinNim(int n) {
         int cnt = 1;
        while(n > 0)
        {
            if((n-1) % 4 == 0)
                n --;
            else if((n-2) % 4 == 0)
                n -= 2;
            else if((n-3) % 4 == 0)
                n -= 3;
            else
                n -= 3;
            if(n <= 3)
                break;
            cnt ++;
        }
        if(cnt % 2 != 0)    return true;
            else return false;
       }
    };
    

    It's response TLE, I can't understand it. My solution is let the remain of stones as possible as be divisible by 4, then count the time. If cnt is odd number , the first people can get the last stone , else he/she can't.


  • 0
    D

    modify like this: int cnt = 0
    delete these: if(n < 3) break;


Log in to reply
 

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