PracticeArrays
Count Readings At or Above the Alert Floor
Context
A monitoring service receives a batch of integer telemetry readings from a device. An alert floor identifies readings that are high enough to qualify for review. Repeated readings represent repeated measurements and must each be considered. Determine how many readings are at least the alert floor.
Problem
Implement a function that counts the readings whose value is greater than or equal to the given alert floor. Check every reading independently, including duplicates, negative values, zero, and readings exactly equal to the floor. If the batch is empty, return zero. The scan must use linear time and constant auxiliary space.
Examples
Example 1
Input:
alert_floor = 7readings = [3, 7, 7, 2, 10]Output:
3Explanation: The readings 7, 7, and 10 meet the floor of 7, so the executed result is 3.
Example 2
Input:
alert_floor = 0readings = [-5, 0, 4, -2, 9]Output:
3Explanation: The readings 0, 4, and 9 meet the floor of 0, so the executed result is 3.
Example 3
Input:
alert_floor = 12readings = []Output:
0Explanation: The input has no readings, so the executed result is 0.
Constraints
0 <= len(readings) <= 100000-1000000 <= readings[i] <= 1000000 for every i-1000000 <= alert_floor <= 1000000- Types:
readingsis int[],alert_flooris int; result is int
Function signature
def count_alert_readings(readings: list[int], alert_floor: int) -> int
readingslist[int]- The integer telemetry readings collected in the batch.
alert_floorint- The inclusive minimum value required for a reading to qualify.
- returns int
- The number of readings whose value is greater than or equal to alert_floor.
Adapted from MBPP problem task_66 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Code
Saved