why my C++ solution does not work?


  • -1
    J
    class Solution {
    public:
        bool canIWin(int maxChoosableInteger, int desiredTotal) {
            int sum = (1+maxChoosableInteger)*maxChoosableInteger/2;
            if(sum < desiredTotal) return false;
            if(desiredTotal <= 0) return true;
    
            vector<bool>visited(maxChoosableInteger);
            map[convert(visited)] = true;
            return dfs(0,maxChoosableInteger,desiredTotal,visited);
        }
    private: 
        bool dfs(int sum,int maxChoosableInteger,int desiredTotal, vector<bool>&visited)
        {
            if(sum>=desiredTotal) return true;
            string _used =convert(visited);
            if(map[_used]) return true;
            for(int i = 1;i<maxChoosableInteger;i++)
            {
                if(!visited[i-1])
                {
                    visited[i-1]= true;
                    if(!dfs(sum+i,maxChoosableInteger,desiredTotal,visited)) 
                    {
                        map[_used] = true;
                        visited[i-1]=false;
                        return true;
                    }
                    visited[i-1]=false;
                }
            }
            map[_used] = false;
            return  map[_used];  
        };
    
    string convert(vector<bool>visisted)
        {
            string res="";
            for(int i = 0;i<visisted.size();i++)
            {
                string tmp = (visisted[i]==true)?"1":"0"; 
                res = res + tmp + "#";
            }
                
            return res.substr(0,res.size()-1);
        }
        unordered_map<string,bool>map;
    
    
    };
    

  • 0
    M

    why my C++ solution does not work?

    Yeah, why doesn't it? Tell us. Does it get some error? Wrong answer? Time limit exceeded? For what test case? Why aren't you telling us? The judge did tell you.


Log in to reply
 

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