Skip to content

Instantly share code, notes, and snippets.

@FelixLuciano
Created March 25, 2021 07:27
Show Gist options
  • Save FelixLuciano/ff4f381ad5d11bfcf6b8e32d6863dc60 to your computer and use it in GitHub Desktop.
Save FelixLuciano/ff4f381ad5d11bfcf6b8e32d6863dc60 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo #188 - Interview question of the week
/**
* From: https://buttondown.email/cassidoo/archive/i-never-dreamed-about-success-i-worked-for-it/
* @param {number[]} m Integer Array.
* @param {number[]} n Integer Array.
* @param {number} k Integer.
* @returns {number} Using the digits from `n` and `m`, return the largest number of length `k`.
* @example
* n = [3,4,6,5]
* m = [9,0,2,5,8,3]
* k = 5
* $ maxNum(n, m, k)
* -> 98655
*/
function maxNum (m, n, k) {
let digits = ""
for (let nums = [...m, ...n], max = [0, 0]; digits.length < k; nums[max[0]] = NaN, digits += max[1], max = [0, 0])
for (let i = 0; i < nums.length; i++)
if (nums[i] > max[1]) max = [i, nums[i]]
return parseInt(digits)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment