Last active
December 24, 2021 23:20
-
-
Save jafar-jabr/ef887ccfd900bbbe527f2b4bfe6d0db5 to your computer and use it in GitHub Desktop.
javascript picking tickets (maxTicket)
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
/* complete the function maxTicket to return the maximum subsqeunces in the given array, | |
where the maximum absilute difference between elemnts in one subsqeunce j and j+1 is either 0 or 1 */ | |
const maxTicket = (s) => { | |
let subsequences = {}; | |
s.forEach((ss) => { | |
subsequences[ss] = subsequences[ss] ? [...subsequences[ss], ss]: [ss]; | |
if(subsequences[ss+1]) { | |
subsequences[ss+1] = [...subsequences[ss+1], ss]; | |
} | |
if(subsequences[ss-1]) { | |
subsequences[ss-1] = [...subsequences[ss-1], ss]; | |
} | |
}); | |
let maxLength = 0; | |
Object.keys(subsequences).forEach((k) => { | |
if(subsequences[k].length > maxLength) maxLength = subsequences[k].length; | |
}) | |
return maxLength; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment