Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Created April 30, 2018 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jakehawken/6f3471963841aaa687b1b25cec436051 to your computer and use it in GitHub Desktop.
Save jakehawken/6f3471963841aaa687b1b25cec436051 to your computer and use it in GitHub Desktop.
For the "almostIncreasingSequence" problem on CodeFights:
func almostIncreasingSequence(sequence: [Int]) -> Bool {
let count = sequence.count
if count < 3 {
return true
}
if sequence.sorted() == sequence {
return isStrictlyAscending(array: sequence)
}
var mutableSequence = sequence
for i in 0..<sequence.count {
let value = mutableSequence.remove(at: i)
if isStrictlyAscending(array: mutableSequence) {
return true
}
mutableSequence.insert(value, at: i)
}
return false
}
func isStrictlyAscending(array: [Int]) -> Bool {
guard array.count > 1 else { return true }
var strictlyAscending = true
var last = array.first!
for i in 1..<array.count {
let current = array[i]
if current <= last {
strictlyAscending = false
break
}
last = current
}
return strictlyAscending
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment