Variable not changing but I know solution is accurate


  • 0
    M
     * @param {string} time
     * @return {string}
     */
    
     var diff = 9007199254740991; 
    
    function calcSeconds (digits){
        var hours = (digits[0]* 10) + digits[1];
        var seconds = (digits[2]*10) + digits[3];
        
        return (hours *3600) + (seconds*60);
    }
    
    function nextClosestTime(time) {
        
        var digits  = [];
        // diff = 9007199254740991; 
       
        var cur = [];
        var ans = [];
        for(var i = 0; i < time.length; i ++){
            if(isFinite(time.charAt(i))){
                digits.push(parseInt(time.charAt(i)));
            }
        }
        
         var target = calcSeconds(digits);
            
       nch(digits, cur, diff, target, ans);
        
        return ans;
       
    };
    
    
    function nch(digits, cur, diff,target, ans){
        if(cur.length == 4){
          
            var curSec = calcSeconds(cur);
            if(Math.abs(curSec - target )<diff){
                console.log(cur);
                diff = Math.abs(calcSeconds(digits) - calcSeconds(cur));
                //ans = cur.slice();
            }
            return;
           
        }
        
        for(var i = 0; i < digits.length; i ++){
            if((((cur[0]*10) + cur[1]) >=24) || (((cur[2]*10) + cur[3]) >=60))
                {
                return;
            }
            
            cur.push(digits[i]);
            nch(digits, cur, diff, target, ans);
            cur.pop()
        }
    
        
    }
    

    I'm not sure why this isn't working, it gets all of the possible solutions, however it gives me every possible solution and not the ones less than the diff variable because the diff variable is never changing


Log in to reply
 

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