---
# System prepended metadata

title: 0941. Valid Mountain Array
tags: [Leetcode, Easy]

---

# 0941. Valid Mountain Array
###### tags: `Leetcode` `Easy`
Link: https://leetcode.com/problems/valid-mountain-array/
## 思路
### 思路一
用两个boolean variable记录是否前面有increase或decrease过
- 两个数相等直接 return $false$
- 如果$decrease=true$，又出现了$increase$则return $false$
- 最后如果$increase=true$ $decrease=true$则return $true$，否则$false$
### 思路二
参考官方解答 直接看code即可
## Code
### 思路一
```java=
class Solution {
    public boolean validMountainArray(int[] arr) {
        if(arr.length < 3) return false;
        boolean increase = false, decrease = false;
        for(int i = 0;i < arr.length-1;i++){
            if(arr[i]==arr[i+1]) return false;
            else if(arr[i]<arr[i+1]){
                if(!decrease) increase = true;
                else return false;
            }
            else if(arr[i]>arr[i+1]){
                decrease = true;
            }
        }
        return increase && decrease;
    }
}
```
### 思路二
```java=
class Solution {
    public boolean validMountainArray(int[] arr) {
        int idx = 0;
        while(idx+1<arr.length && arr[idx]<arr[idx+1]){
            idx++;
        }
        if(idx+1==arr.length || idx==0 || arr[idx]==arr[idx+1]) return false;
        while(idx+1<arr.length && arr[idx]>arr[idx+1]){
            idx++;
        }
        return idx+1==arr.length;
    }
}
```