Last active
May 13, 2021 18:20
-
-
Save chygoz2/9b215f2ede3d6e0a5a3df1ad707eb8fc to your computer and use it in GitHub Desktop.
Algorithm Fridays 5 solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mergeAges = (classA: Array<number>, classB: Array<number>) => { | |
let mergedAges: Array<number> = [] | |
let i = 0, j = 0 | |
while(i < classA.length && j < classB.length) { | |
if (isNaN(classA[i]) || isNaN(classB[j])) throw new Error('Invalid input') | |
if (classA[i] <= classB[j]) { | |
mergedAges.push(classA[i++]) | |
} else { | |
mergedAges.push(classB[j++]) | |
} | |
} | |
if (i < classA.length) { | |
for (; i < classA.length; i++) { | |
if (isNaN(classA[i])) throw new Error('Invalid input') | |
mergedAges.push(classA[i]) | |
} | |
} | |
if (j < classB.length) { | |
for (; j < classB.length; j++) { | |
if (isNaN(classB[j])) throw new Error('Invalid input') | |
mergedAges.push(classB[j]) | |
} | |
} | |
return mergedAges | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @chygoz2, thank you for participating in Week 5 of #AlgorithmFridays.
Congratulations! 🎉🎉, your solution has been selected as one of the 12 winning solutions for Week 5 of #AlgorithmFridays.
Your solution was selected because it is clean, robust, had the best time-efficiency and passes all of the test cases. I particularly like your error checks for each element in the array. Really clean!
However since only 3 solutions can be awarded the $20 prize, there will be a 'raffle draw' tomorrow at 9.00 am WAT (1.00 am PST) to select 3 out of the 12 solutions in a fair manner.
If you will to participate in the raffle draw, please send an email to uduak@meekg33k.dev or send me a DM on Twitter @meekg33k so I can share the meeting link with you.
Congratulations once again and thank you for participating you. See you tomorrow for Week 6 of #AlgorithmFridays.