Clean JavaScript Solution


  • 2
    B
    var largestDividable = (area) => {
      for (let i = Math.sqrt(area) | 0; i > 0; i--) {
        if (area % i === 0) return i;
      }
    }
    
    var constructRectangle = function(area) {
      if (area === 0) return [0, 0];
    
      let w = largestDividable(area);
      let l = area / w;
      return [l, w];
    };
    

  • 0
    R

    This is a little shorter:

    var constructRectangle = function(area) {
        var w = Math.sqrt(area) >> 0;
        while(area % w != 0) w--;
        return [(area/w)>>0, w]
    };
    

  • 1
    B

    Not sure why is the obsession with 'sort' solutions ;)
    Nice job though.

    I definitely prefer | 0 to >> 0 and it is the way asm.js does it.


Log in to reply
 

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