← 返回 meta 的题目列表Number of Direction Changes in an Array
类型:online_judge
meta
Given an integer array, define 'change direction' as the change in monotonicity (strictly greater or less than the previous number in the array). Design an algorithm to return the total number of direction changes.
Example 1: (edge case)
Input: [1, 1, 1, 1]
Output: 1
Explanation: The array is not empty, so the output must be at least 1 (even though there's no direction change).
Example 2: (edge case)
Input: []
Output: 0
Example 3: (normal case)
Input: [1, 3, 2, 5, 6]
Output: 4
Explanation: 1, 1->3, 3->2, 2->5.
Example 4: (special case)
Input: [2, 2, 1, 3]
Output: 3
Explanation: 2, 2->1, 1->3.
Example
Input
1 1 1 1