C++ TLE Need help


  • 0
    T

    Below is my pdfs code and it has TLE error. Can anyone help me improve it? Thanks!!

    public:
    vector<vector<int> > permuteUnique(vector<int> &num) {
        sort(num.begin(),num.end());
    	vector<int> path;
    	vector<vector<int>> result;
    	unordered_map<int,int> m;
    	for(int i = 0; i<num.size();i++){
    	    if(m.find(num[i])==m.end())
    	        m[num[i]] = 1;
    	    else
    	        m[num[i]]++;
    	}
    	
    	dfs(num,path,result,m);
    	return result;
    }
    
    private:
    void dfs(vector<int> &num, vector<int> path, vector<vector<int>> &result,unordered_map<int,int> &m){
    	if(path.size()==num.size()){
    	    if(find(result.begin(),result.end(),path)==result.end())
        	result.push_back(path);
    		return;
    	}
    
    	for(int i = 0; i<num.size();i++){
    		if(count(path.begin(),path.end(),num[i])<m[num[i]]){
    			path.push_back(num[i]);
    			dfs(num,path,result,m);
    			path.pop_back();
    		}
    	}
    }

Log in to reply
 

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