Skip to content

Instantly share code, notes, and snippets.

@jafar-jabr
Last active December 24, 2021 23:20
Show Gist options
  • Save jafar-jabr/ef887ccfd900bbbe527f2b4bfe6d0db5 to your computer and use it in GitHub Desktop.
Save jafar-jabr/ef887ccfd900bbbe527f2b4bfe6d0db5 to your computer and use it in GitHub Desktop.
javascript picking tickets (maxTicket)
/* 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