-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
941 Valid Mountain Array.swift
41 lines (32 loc) 路 1.02 KB
/
941 Valid Mountain Array.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// 941 Valid Mountain Array.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 10/12/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/valid-mountain-array/
class Solution {
/// Finds if and only if the given array is a valid mountain array.
///
/// - Parameter A: An array of integers.
/// - Returns: True if `A` is a valid mountain array, otherwise returns false.
///
/// - Complexity:
/// - time: O(n), where n is the length of `A`.
/// - space: O(1), only constant space is used.
func validMountainArray(_ A: [Int]) -> Bool {
let n = A.count
guard n >= 3 else { return false }
var i = 0
while (i + 1) < n, A[i] < A[i+1] {
i += 1
}
guard i != 0, i != n - 1 else { return false }
while (i + 1) < n, A[i] > A[i+1] {
i += 1
}
return i == n - 1
}
}