- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmountain_array_valid.py
More file actions
Latest commit
41 lines (36 loc) · 1.03 KB
/
Copy pathmountain_array_valid.py
File metadata and controls
41 lines (36 loc) · 1.03 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
"""
https://leetcode.com/problems/find-in-mountain-array/
"""
importunittest
fromtypingimportList
defvalid(nums: List[int]) ->bool:
length=len(nums)
iflength<3:
returnFalse
# 是否单调递增
last_item_is_increasing=nums[0] <nums[1]
ifnotlast_item_is_increasing:
returnFalse
foriinrange(2, length):
ifnums[i-1] >nums[i]:
last_item_is_increasing=False
elifnums[i-1] <nums[i]:
# 山脉数组不允许先递减(False)后递增(True)
ifnotlast_item_is_increasing:
returnFalse
last_item_is_increasing=True
else:
returnFalse
ifnotlast_item_is_increasing:
returnTrue
else:
returnFalse
classUnitTest(unittest.TestCase):
TEST_CASES= [
([2, 1], False),
([3, 5, 5], False),
([0, 3, 2, 1], True),
]
deftest_valid(self):
fornums, expectedinself.TEST_CASES:
self.assertEqual(expected, valid(nums))