I have started the Hackerrank interview preparation kit. The first problem I have solved with Swift is as follows:
Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. She must avoid the thunderheads. Determine the minimum number of jumps it will take Emma to jump from her starting position to the last cloud.
For each game, Emma will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided.
func jumpingOnClouds(c: [Int]) -> Int {
var jump = 0
var location = 0
repeat {
if (location + 2 < c.count) &&
(c[location + 2] == 0) {
location += 2
} else {
location += 1
}
if location >= c.count {
break
}
jump += 1
} while location < c.count
return jump
}
It's time complexity is \$O(n)\$. Are there any ways I could reduce this? I am also interested in improving the code quality. Are there ways I can take better advantage of Swift to improve my code?