2960. Count Tested Devices After Test Operations LeetCode Solution

In this guide, you will get 2960. Count Tested Devices After Test Operations LeetCode Solution with the best time and space complexity. The solution to Count Tested Devices After Test Operations problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Tested Devices After Test Operations solution in C++
  4. Count Tested Devices After Test Operations solution in Java
  5. Count Tested Devices After Test Operations solution in Python
  6. Additional Resources
2960. Count Tested Devices After Test Operations LeetCode Solution image

Problem Statement of 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:

Input: batteryPercentages = [1,1,2,1,3]
Output: 3
Explanation: Performing the test operations in order starting from device 0:
At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2].
At device 1, batteryPercentages[1] == 0, so we move to the next device without testing.
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1].
At device 3, batteryPercentages[3] == 0, so we move to the next device without testing.
At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same.
So, the answer is 3.

See also  3304. Find the K-th Character in String Game I LeetCode Solution

Example 2:

Input: batteryPercentages = [0,1,2]
Output: 2
Explanation: Performing the test operations in order starting from device 0:
At device 0, batteryPercentages[0] == 0, so we move to the next device without testing.
At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1].
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same.
So, the answer is 2.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2960. Count Tested Devices After Test Operations LeetCode Solution in C++

class Solution {
 public:
  int countTestedDevices(vector<int>& batteryPercentages) {
    int ans = 0;

    for (const int batteryPercentage : batteryPercentages)
      if (batteryPercentage - ans > 0)
        ++ans;

    return ans;
  }
};
/* code provided by PROGIEZ */

2960. Count Tested Devices After Test Operations LeetCode Solution in Java

class Solution {
  public int countTestedDevices(int[] batteryPercentages) {
    int ans = 0;

    for (final int batteryPercentage : batteryPercentages)
      if (batteryPercentage - ans > 0)
        ++ans;

    return ans;
  }
}
// code provided by PROGIEZ

2960. Count Tested Devices After Test Operations LeetCode Solution in Python

class Solution:
  def countTestedDevices(self, batteryPercentages: list[int]) -> int:
    ans = 0

    for batteryPercentage in batteryPercentages:
      if batteryPercentage - ans > 0:
        ans += 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.