Why does my javascript "run code" always finish <300ms with right answer for any "submit solution" that TLEs?


  • 0
    J

    My code is brute force with two optimizations:

    • it starts with the lowest-valued entries and works up from there
    • it ignores paths once it's visited that point from a lower start value, very likely minimizing retread

    All submissions get TLE on different sets, yet run <300ms with any erring custom test case. Any ideas?

    var longestIncreasingPath = function(m) {
        var max=0
            ,lowestVisitedAt={}
            ,ijByValAsc=[]
        
        function walk(i,j,dist,startingFrom){//,path){
            var ij=[i,j]+''
            if(lowestVisitedAt[ij]<startingFrom) return
            lowestVisitedAt[ij]=Math.min(lowestVisitedAt[ij],startingFrom)
            //will be in order: up right down left
            var paths=[
                     m[i+1]!==undefined && m[i+1][j  ]!==undefined ? m[i+1][j  ] : false
                    ,m[i  ]!==undefined && m[i  ][j+1]!==undefined ? m[i  ][j+1] : false
                    ,m[i-1]!==undefined && m[i-1][j  ]!==undefined ? m[i-1][j  ] : false
                    ,m[i  ]!==undefined && m[i  ][j-1]!==undefined ? m[i  ][j-1] : false
                ]
                ,recur=false
            paths.forEach((x,ii)=>{
                var  pi= i+(ii==0? 1 : ii==2 ? -1 : 0)
                    ,pj= j+(ii==1? 1 : ii==3 ? -1 : 0)
                //only if an increase
                if(x && m[pi][pj]>m[i][j]){
                    walk(pi,pj,dist+1,startingFrom)//,path.concat(m[pi][pj]))
                    recur=true
                }
            })
            
            if(!recur){
                //console.log(path)
                max=Math.max(dist,max)
            }
        }
        //once walker established, prioritize by starting at the lowest values
        //first gather items
        m.forEach((row,i)=>{
            row.forEach((col,j)=>{
                ijByValAsc.push({ij:[i,j],v:col})
            })
        })
        //then sort & go
        ijByValAsc
            .sort((a,b)=>a.v - b.v)
            .forEach(o=>{
                walk(...o.ij,1,o.v)//,[o.v])
            })
        //track covering can minimize paths taken
        return max
        
    };

  • 1

    Why does my javascript "run code" always finish <300ms with right answer for any "submit solution" that TLEs?

    Because it's fast enough for each single test but not fast enough for the whole test suite, which contains 137 tests.

    Print the test number to see that you make it through around 125 tests:

    tests = 0
    var longestIncreasingPath = function(m) {
        tests++
        console.log(tests)
        ...
    

  • 0
    J

    Thanks.

    Does that imply as new test cases are added to other problems, ALL previous submissions are re-run to properly place them on the new bell curve?

    I was under the impression tests were timed individually. I didn't get how a single millisecond number was returned if that was the case, but didn't think it warranted the leap to "the whole suite is timed at once"...maybe "just the longest" was shown.

    So there is a drastic timing difference between "run code" (once) and "submit solution" (to be timed as an arbitrary number of runs). This may explain a lot.


  • 0

    I think submissions aren't re-run automatically, but the statistics are reset regularly, like once a week.


Log in to reply
 

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