Skip to content

Commit 19fa5d6

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.7 MB (64.44%)
1 parent 253ddab commit 19fa5d6

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. An index <code>i</code> is part of a <strong>hill</strong> in <code>nums</code> if the closest non-equal neighbors of <code>i</code> are smaller than <code>nums[i]</code>. Similarly, an index <code>i</code> is part of a <strong>valley</strong> in <code>nums</code> if the closest non-equal neighbors of <code>i</code> are larger than <code>nums[i]</code>. Adjacent indices <code>i</code> and <code>j</code> are part of the <strong>same</strong> hill or valley if <code>nums[i] == nums[j]</code>.</p>
2+
3+
<p>Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on <strong>both</strong> the left and right of the index.</p>
4+
5+
<p>Return <i>the number of hills and valleys in </i><code>nums</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> nums = [2,4,1,1,6,5]
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong>
14+
At index 0: There is no non-equal neighbor of 2 on the left, so index 0 is neither a hill nor a valley.
15+
At index 1: The closest non-equal neighbors of 4 are 2 and 1. Since 4 &gt; 2 and 4 &gt; 1, index 1 is a hill.
16+
At index 2: The closest non-equal neighbors of 1 are 4 and 6. Since 1 &lt; 4 and 1 &lt; 6, index 2 is a valley.
17+
At index 3: The closest non-equal neighbors of 1 are 4 and 6. Since 1 &lt; 4 and 1 &lt; 6, index 3 is a valley, but note that it is part of the same valley as index 2.
18+
At index 4: The closest non-equal neighbors of 6 are 1 and 5. Since 6 &gt; 1 and 6 &gt; 5, index 4 is a hill.
19+
At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley.
20+
There are 3 hills and valleys so we return 3.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [6,6,5,5,4,1]
27+
<strong>Output:</strong> 0
28+
<strong>Explanation:</strong>
29+
At index 0: There is no non-equal neighbor of 6 on the left, so index 0 is neither a hill nor a valley.
30+
At index 1: There is no non-equal neighbor of 6 on the left, so index 1 is neither a hill nor a valley.
31+
At index 2: The closest non-equal neighbors of 5 are 6 and 4. Since 5 &lt; 6 and 5 &gt; 4, index 2 is neither a hill nor a valley.
32+
At index 3: The closest non-equal neighbors of 5 are 6 and 4. Since 5 &lt; 6 and 5 &gt; 4, index 3 is neither a hill nor a valley.
33+
At index 4: The closest non-equal neighbors of 4 are 5 and 1. Since 4 &lt; 5 and 4 &gt; 1, index 4 is neither a hill nor a valley.
34+
At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.
35+
There are 0 hills and valleys so we return 0.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>3 &lt;= nums.length &lt;= 100</code></li>
43+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
44+
</ul>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution:
5+
def countHillValley(self, nums: List[int]) -> int:
6+
# Remove consecutive duplicates
7+
filtered = []
8+
for num in nums:
9+
if not filtered or filtered[-1] != num:
10+
filtered.append(num)
11+
12+
count = 0
13+
for i in range(1, len(filtered) - 1):
14+
# Hill condition
15+
if filtered[i] > filtered[i - 1] and filtered[i] > filtered[i + 1]:
16+
count += 1
17+
# Valley condition
18+
elif filtered[i] < filtered[i - 1] and filtered[i] < filtered[i + 1]:
19+
count += 1
20+
21+
return count
22+

0 commit comments

Comments
 (0)