Count Tested Devices After Test Operations

You are given a 0-indexed integer array batteryPercentages having length n, denoting the battery percentages of n 0-indexed devices.

Your task is to test each device i in order from 0 to n - 1, by performing the following test operations:

  • If batteryPercentages[i] is greater than 0:
  • Increment the count of tested devices.
  • Decrease the battery percentage of all devices with indices j in the range [i + 1, n - 1] by 1, ensuring their battery percentage never goes below 0, i.e., batteryPercentages[j] = max(0, batteryPercentages[j] - 1).
  • Move to the next device.
  • Otherwise, move to the next device without performing any test.

Return an integer denoting the number of devices that will be tested after performing the test operations in order.

Example 1
InputbatteryPercentages = [1,1,2,1,3]
Output3
Performing the test operations in order tests devices 0, 2, and 4, so the answer is 3.
Example 2
InputbatteryPercentages = [0,1,2]
Output2
Performing the test operations in order tests devices 1 and 2, so the answer is 2.

Constraints

  • 1 <= n == batteryPercentages.length <= 100
  • 0 <= batteryPercentages[i] <= 100

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate