Solution 1: Intuitive
func peakIndexInMountainArray(_ A: [Int]) -> Int {
for i in stride(from: 1, to: A.count, by: 1) {
if A[i] < A[i-1] {
return i-1
}
}
return -1
}
Solution 2: Binary Search
func peakIndexInMountainArray(_ A: [Int]) -> Int {
var start = 0
var end = A.count - 1
while start < end {
let mid = start + (end - start) / 2
if A[mid] < A[mid+1] {
start = mid + 1
} else {
end = mid
}
}
return start
}