I was going about solving some algos on binarysearch.io and came across this problem:
I solved it using JS as follows:
class Solution { sol