Find maximum possible time HH:MM by permuting four given digits

前端 未结 23 2060
执念已碎
执念已碎 2020-11-30 02:44

I recently took a coding test for a promotion at work. This was one of the tasks I really struggled with and was wondering what is the best way to do this. I used a load of

23条回答
  •  离开以前
    2020-11-30 02:59

    The idea:

    • Find all combinations array (24 total)
    • filter out all invalid combinations (time format)
    • find the time value
    • output the array with the max time value

    Solution:

    First allCom will return all combination of the 4 number (total 24 combinations)

    Then for the 24 array (combinations) call .forEach go through each array check if it is a valid time format. If it is valid time format then calculate the time value with

    If the time is AB:CD then the value:

    A = A * 10 hours = A * 10 * 3600s = A * 36000s

    B = B * 1 hour = B * 3600s

    C = C * 10s

    D = D

    Total value = A*36000 + B*3600 + C*10 + D

    Now you got the value of the current array, compare with the saved Max, replace the max if this value is bigger.

    At the end of the loop determine if a max found or it is not valid.

    generate(6, 5, 2, 0);
    generate(3, 9, 5, 0);
    generate(7, 6, 3, 8);
    generate(1, 7, 2, 7);
    generate(1, 1, 1, 2);
    
    // return all combination of 4 number (24 combination total)
    function allCom(inputArray) {
      var result = inputArray.reduce(function permute(res, item, key, arr) {
        return res.concat(arr.length > 1 && arr.slice(0, key).concat(arr.slice(key + 1)).reduce(permute, []).map(function(perm) {
          return [item].concat(perm);
        }) || item);
      }, []);
      return result;
    }
    
    // core function to determine the max comb
    function generate(A, B, C, D) {
      let input = [A, B, C, D];
      let allComb = allCom(input);
      let max = '';
      let maxA = [];
    
      allComb.forEach(function(comb, index, arr) {
        if (validCom(comb)) {
          let temp = calValue(comb);
          maxA = temp > max ? comb : maxA;
          max = temp > max ? temp : max;
        }
        if (index == allComb.length - 1) {
          if (max) {
            return console.log('For ' + JSON.stringify(input) + ' found max comb: ' + maxA[0] + maxA[1] + ':' + maxA[2] + maxA[3]);
          }
          return console.log('Sorry ' + JSON.stringify(input) + ' is not valid');
        }
      });
    }
    
    // check if this array is valid time format, ex [1,2,9,0] false, [2,2,5,5] true
    function validCom(ar) {
      if (ar[0] <= 2 && ((ar[0] == 2 && ar[1] < 4) || (ar[0] != 2 && ar[1] <= 9)) && ar[2] <= 5 && ar[3] <= 9) {
        return true;
      }
      return false;
    }
    
    // calculate the total value of this comb array
    function calValue(ar) {
      return +ar[0] * 36000 + +ar[1] * 3600 + +ar[2] * 10 + +ar[0];
    }
    
    
    $('button').on('click', function(e) {
        let inp = $('select');
        generate(inp[0].value, inp[1].value, inp[2].value, inp[3].value);
    });
    
    
    var s = $('
                            
        
    提交评论

提交回复
热议问题