Created
April 30, 2018 01:44
-
-
Save jakehawken/6f3471963841aaa687b1b25cec436051 to your computer and use it in GitHub Desktop.
For the "almostIncreasingSequence" problem on CodeFights:
This file contains 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
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