Skip to content

Commit e788b01

Browse files
committed
docs: update a description of the solution to lc problem: No.2210
No.2210.Count Hills and Valleys in an Array
1 parent 5e65775 commit e788b01

File tree

1 file changed

+18
-2
lines changed
  • solution/2200-2299/2210.Count Hills and Valleys in an Array

1 file changed

+18
-2
lines changed

solution/2200-2299/2210.Count Hills and Valleys in an Array/README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,27 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59-
先处理数组 `nums`把连续的都变成一个,比如 `[6, 6, 5, 5, 4, 1]`,转换为 `[6, 5, 4, 1]` 之后,再进行比较
59+
先处理数组 `nums`对于相邻且重复出现的元素,只保留其中一个,如 `[6, 6, 5, 5, 4, 1]`,转换为 `[6, 5, 4, 1]`,再依照题意,进行统计
6060

6161
优化:
6262

63-
可以使用双指针的方式,忽略相邻重复的元素,而无需改动原数组。
63+
上述处理的数组方式,不论是删除元素还是新开数组,都会造成复杂度的提升。而实际上,只需要忽略相邻重复元素即可,无需改动原数组。
64+
65+
```txt
66+
COUNT_HILL_VALLEY(A)
67+
n = A.length
68+
r = 0
69+
p = A[0]
70+
for i = 1 in n - 1
71+
c = A[i]
72+
q = A[i + 1]
73+
if c == q
74+
continue
75+
if c > prev && c > q || c < prev && c < q
76+
r += 1
77+
p = c
78+
return r
79+
```
6480

6581
<!-- tabs:start -->
6682

0 commit comments

Comments
 (0)