forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
47 lines (34 loc) · 879 Bytes
/
main.cpp
File metadata and controls
47 lines (34 loc) · 879 Bytes
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
42
43
44
45
46
47
/// Source : https://leetcode.com/problems/valid-mountain-array/
/// Author : liuyubobobo
/// Time : 2018-11-17
#include <iostream>
#include <vector>
using namespace std;
/// One Pass
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
bool validMountainArray(vector<int>& A) {
if(A.size() < 3)
return false;
int i;
for(i = 1; i < A.size(); i ++)
if(A[i] < A[i - 1])
break;
else if(A[i] == A[i - 1])
return false;
if(i == 1 || i == A.size())
return false;
for(int j = i; j < A.size(); j ++)
if(A[j - 1] <= A[j])
return false;
return true;
}
};
int main() {
vector<int> A1 = {0, 3, 2, 1};
cout << Solution().validMountainArray(A1) << endl;
// true
return 0;
}