Please tell why my solution is getting TLE (recursive solution C++)


  • 0
    K

    The code is giving correct answers. Why this TLE?

    class Solution {
    public:

    string result;
    
    void getPermutationRec(int n,int begin,int &k,string res)
    {
        if(begin==n)
        k--;
        
        if(!k)
        {
            result=res;
            return;
        }
        
        for(int i=begin;i<n && k>0;i++)
        {
            swap(res[i],res[begin]);
            getPermutationRec(n,begin+1,k,res);
        }
    }
    string getPermutation(int n, int k) {
        string res="";
        
        for(int i=1;i<=n;i++)
        res+=to_string(i);
        getPermutationRec(n,0,k,res);
        
        return result;
    }
    

    };


Log in to reply
 

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