Skip to content

Instantly share code, notes, and snippets.

@germanescobar
Created August 20, 2022 00:39
Show Gist options
  • Save germanescobar/65537f2bcd217a456243fc3ea873aede to your computer and use it in GitHub Desktop.
Save germanescobar/65537f2bcd217a456243fc3ea873aede to your computer and use it in GitHub Desktop.
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