Threshold alerts
EasyOnline Assessment
Count how many times a series of readings crosses up to a threshold.
An alert is emitted at reading i when readings[i] >= threshold and the previous reading was below threshold (readings[i - 1] < threshold). The first reading has no previous reading: if it is at or above the threshold it emits an alert too. Staying at or above the threshold does not emit further alerts.
Input: readings, a list of integers, and threshold, an integer.
Output: an integer, the number of alerts. An empty list returns 0.
Example 1
- Input:
- [[4,5,8,2,5],5]
- Output:
- 2
- Explanation
- With threshold 5, the 5 after 4 is an alert (4 is below 5). The 8 comes right after a 5, so it is not a new crossing. The final 5 follows a 2, which is below 5, so it is an alert. Total: 2.
Example 2
- Input:
- [[7,3,9,1,10],5]
- Output:
- 3
- Explanation
- The first reading 7 is at or above 5, so it alerts. 3 is below. 9 follows 3, so it alerts. 1 is below. 10 follows 1, so it alerts. Total: 3.
Example 3
- Input:
- [[3,3,3],3]
- Output:
- 1
- Explanation
- The first reading 3 equals the threshold, so it alerts. The next two readings follow a reading that is not below the threshold, so they do not. Total: 1.
Constraints
0 <= readings.length <= 10,000-1,000,000 <= readings[i], threshold <= 1,000,000