Created
November 24, 2018 04:50
-
-
Save fpdjsns/202c786483f921318102ce2c6e8fe8bf to your computer and use it in GitHub Desktop.
[leetcode] 941. Valid Mountain Array : https://leetcode.com/problems/valid-mountain-array/
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
class Solution { | |
public: | |
int SAME = 0; | |
int UP = 1; | |
int DOWN = -1; | |
int getStatus(int diff){ | |
if(diff == 0) return SAME; | |
return diff > 0 ? UP : DOWN; | |
} | |
bool validMountainArray(vector<int>& A) { | |
if(A.size() < 3 || A[0] > A[1]) return false; | |
bool isUp = true; | |
for(int i=1;i<A.size();i++){ | |
int status = getStatus(A[i] - A[i-1]); | |
if(status == SAME) return false; | |
else if(status == UP){ | |
if(!isUp) return false; | |
}else{ // DOWN | |
if(isUp) isUp = false; | |
} | |
} | |
return !isUp; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment