Created
August 20, 2022 00:39
-
-
Save germanescobar/65537f2bcd217a456243fc3ea873aede to your computer and use it in GitHub Desktop.
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
var isPossible = function(nums) { | |
if (nums.length === 0) return true | |
if (nums.length < 3) return false | |
const clone = [...nums.slice(1)] | |
const seq = [nums[0]] | |
for (let i=0; i < nums.length && seq.length < 3; i++) { | |
const next = seq[seq.length - 1] + 1 | |
if (nums[i] === next) { | |
clone.splice(i-seq.length, 1) | |
seq.push(nums[i]) | |
} | |
} | |
if (seq.length < 3) return false | |
if (isPossible(clone)) return true | |
for (let i=0; i < clone.length; i++) { | |
const next = seq[seq.length - 1] + 1 | |
if (clone[i] === next) { | |
seq.push(clone[i]) | |
clone.splice(i, 1) | |
i-- | |
if (isPossible(clone)) return true | |
} | |
} | |
return false | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment