Skip to content

Commit 7d76953

Browse files
authored
Create count-hills-and-valleys-in-an-array.py
1 parent 7b32925 commit 7d76953

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# simulation, array
5+
class Solution(object):
6+
def countHillValley(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: int
10+
"""
11+
result, inc = 0, -1
12+
for i in xrange(len(nums)-1):
13+
if nums[i] < nums[i+1]:
14+
result += int(inc == 0)
15+
inc = 1
16+
elif nums[i] > nums[i+1]:
17+
result += int(inc == 1)
18+
inc = 0
19+
return result

0 commit comments

Comments
 (0)