SHARE
TWEET

Untitled

a guest Oct 10th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var nthUglyNumber = function(n, a, b, c) {
  2.     const inputs = [a, b, c];
  3.     const curr = [a, b, c];
  4.    
  5.     let counter = n;
  6.    
  7.     while (counter !== 1) {
  8.         const sorted = curr.slice().sort((a, b) => a > b ? 1 : -1);
  9.         let min = sorted[0];
  10.         let minIndex = curr.findIndex((a) => a === min);
  11.         let mid = sorted[1];
  12.        
  13.         let step = Math.floor((mid - min) / inputs[minIndex]) - 1;
  14.         step = Math.min(step, counter - 1);  
  15.         step = Math.max(step, 1);
  16.        
  17.         curr[minIndex] += inputs[minIndex] * step;
  18.        
  19.         counter -= step;
  20.        
  21.         for (let i = 0; i < curr.length; ++i) {
  22.             if (curr[i] === min) {
  23.                 curr[i] += inputs[i];
  24.             }
  25.         }        
  26.     }
  27.    
  28.     return Math.min(...curr);
  29.    
  30. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top