-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path941.valid-mountain-array.py
More file actions
72 lines (63 loc) · 1.24 KB
/
941.valid-mountain-array.py
File metadata and controls
72 lines (63 loc) · 1.24 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#
# @lc app=leetcode id=941 lang=python3
#
# [941] Valid Mountain Array
#
# https://leetcode.com/problems/valid-mountain-array/description/
#
# algorithms
# Easy (33.63%)
# Likes: 724
# Dislikes: 88
# Total Accepted: 122.4K
# Total Submissions: 364K
# Testcase Example: '[2,1]'
#
# Given an array of integers arr, return true if and only if it is a valid
# mountain array.
#
# Recall that arr is a mountain array if and only if:
#
#
# arr.length >= 3
# There exists some i with 0 < i < arr.length - 1 such that:
#
# arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
# arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
#
#
#
#
#
# Example 1:
# Input: arr = [2,1]
# Output: false
# Example 2:
# Input: arr = [3,5,5]
# Output: false
# Example 3:
# Input: arr = [0,3,2,1]
# Output: true
#
#
# Constraints:
#
#
# 1 <= arr.length <= 10^4
# 0 <= arr[i] <= 10^4
#
#
#
# @lc code=start
class Solution:
def validMountainArray(self, arr: List[int]) -> bool:
i = 0
l = len(arr)
while i + 1 < l and arr[i] < arr[i+1]:
i += 1
if i == 0 or i == l-1:
return False
while i+1 < l and arr[i] > arr[i+1]:
i+=1
return i == l-1
# @lc code=end