Lexicographical Number TLE ??


  • 0
    H

    Guys, I have issue with the TLE in my algorithm, Can you point where can I further optimism it? It uses the basic reclusive backtracking and I believe it does the minimum steps to generate the results. I even have did the output to check if its indeed a call issue but still not able to understand. Works fine, its not able to handle large input thats what it says

    CODE:-

    public class Solution {

    public boolean helper(int n, int cnt, List<Integer> lst){
        if(cnt <= n) {
             // System.out.println("Added "+cnt);
              for(int i=0; i<= 9; i++){
                   String val;
                   if(cnt == 0){
                       if(i == 9) // This check removes duplicate creation
                           break; 
                       val = ""+ (i+1); 
                   }  else {
                       val  = "" + cnt + i;
                   }
                   //System.out.println("New String "+val);
                   int cal = Integer.parseInt(val);
                   lst.add(cal);
              
                   if(!helper(n, cal, lst)) {
                    //   System.out.println("Removing String "+val);
                       lst.remove(new Integer(cal));
                       break;
                   }
               }
        } else {     
            return false;
        }
        return true;    
    }
    
    public List<Integer> lexicalOrder(int n) {
        List<Integer> lst = new ArrayList<Integer>();
        helper(n, 0, lst);
        return lst;
    }
    

    }


Log in to reply
 

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