返回介绍

solution / 2900-2999 / 2960.Count Tested Devices After Test Operations / README_EN

发布于 2024-06-17 01:02:58 字数 4962 浏览 0 评论 0 收藏 0

2960. Count Tested Devices After Test Operations

中文文档

Description

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.

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

Solutions

Solution 1: Simulation

Assume that the current number of devices we have tested is $ans$. When testing a new device $i$, its remaining battery is $\max(0, batteryPercentages[i] - ans)$. If the remaining battery is greater than $0$, it means this device can be tested, and we need to increase $ans$ by $1$.

Finally, return $ans$.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

class Solution:
  def countTestedDevices(self, batteryPercentages: List[int]) -> int:
    ans = 0
    for x in batteryPercentages:
      x -= ans
      ans += x > 0
    return ans
class Solution {
  public int countTestedDevices(int[] batteryPercentages) {
    int ans = 0;
    for (int x : batteryPercentages) {
      x -= ans;
      if (x > 0) {
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countTestedDevices(vector<int>& batteryPercentages) {
    int ans = 0;
    for (int x : batteryPercentages) {
      x -= ans;
      if (x > 0) {
        ++ans;
      }
    }
    return ans;
  }
};
func countTestedDevices(batteryPercentages []int) (ans int) {
  for _, x := range batteryPercentages {
    x -= ans
    if x > 0 {
      ans++
    }
  }
  return
}
function countTestedDevices(batteryPercentages: number[]): number {
  let ans = 0;
  for (let x of batteryPercentages) {
    x -= ans;
    if (x > 0) {
      ++ans;
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文