Leetcode Note: Go - Count Hills and Valleys in an Array
Count Hills and Valleys in an Array - LeetCode
https://leetcode.com/problems/count-hills-and-valleys-in-an-array/
- Go 言語で取り組んだメモ
回答
C++ | Golang - Count Hills and Valleys in an Array - LeetCode
https://leetcode.com/problems/count-hills-and-valleys-in-an-array/solutions/3355594/c-golang/
func countHillValley(nums []int) int {
var n, prev, ans int = len(nums), nums[0], 0
for i := 1; i < n; i++ {
for i < n && nums[i] == nums[i - 1] {i++}
if i < n && ((nums[i - 1] > nums[i] && nums[i - 1] > prev) || (nums[i - 1] < nums[i] && nums[i - 1] < prev)) {
ans++
}
prev = nums[i - 1]
}
return ans
}