Created
July 17, 2019 11:23
-
-
Save LucienLee/3031c627a20ff9c57ffb426fa4da3c89 to your computer and use it in GitHub Desktop.
find longest bi-value sequence
This file contains hidden or 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
function solution(A) { | |
let mark = new Set(); | |
let curLength = 0; | |
let consecutiveSeq = 1; | |
let allSeqLength = []; | |
for(let i = 0; i < A.length; i++) { | |
let num = A[i]; | |
if (!mark.has(num) && mark.size < 2) { | |
curLength++; | |
mark.add(num); | |
} else if (!mark.has(num) && mark.size === 2) { | |
// save previous seq | |
allSeqLength.push(curLength); | |
curLength = consecutiveSeq + 1; | |
mark = new Set([A[i - 1], num]); | |
consecutiveSeq = 1; | |
} else { | |
curLength++; | |
consecutiveSeq = num === A[i - 1] ? consecutiveSeq + 1 : 1; | |
} | |
} | |
allSeqLength.push(curLength); | |
return Math.max(...allSeqLength); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rewrote in python