Next Bigger Number

const {log} = console;

/*
    n(abcde): 
      代表下一個比 abcde 大的數字 (但還是只能用 abcde 這幾個數字)。
      
    f(abcde): 
      1. 代表在 bcde 內找出「比 a 大的數字最小者」,然後跟 a 對調,
         如果找不到,就回傳 undefined。
      2. 對調後,後面的數字「從小排到大」,跟第一個數字接起來,然後回傳。
      
    n(abcde) = abc+f(de) || ab+f(cde) || a+f(bcde) || f(abcde)
*/

// assume: `k` -> string of digits.
// suppose k = abcd, b = next bigger digit than `a`, then:
// f(abcd) = b + smallest(acd)
function f(k) {
    
    // 1. find next bigger digit than leading digit
    let arr = k.substring(1).split('').sort();  // sort is key
    let i = arr.findIndex(c => +c > +k[0]);
    
    // 2. if not found, return undefined.
    if (i === -1) return undefined;
    
    //    if found, interchange them.
    let b = arr[i]; arr[i] = k[0];
    return b + arr.sort().join('');             // remember to sort again
}

// assume: `k` -> string of digits.
// next bigger number
function n(k) {
    
    let next;
    let left = k.length - 2;
    
    while (!next && left >= 0) {
        let found = f(k.substring(left));
        if (found) next = k.substring(0, left) + found;
        left -= 1;
    }
    
    return next;
}

// -------- log --------

// f(k)
[
    3412,           // 4123
    4312,           // undefined
]
    .map(n => f(String(n)))
    .forEach(x => log(x));

// n(k)
[
    3412,           // 3421
    4312,           // 4321
    59884848459853, // 59884848483559
]
    .map(k => n(String(k)))
    .forEach(x => log(x));

Last updated