LeetCode-2960. 统计已测试设备【数组 模拟】

题目描述:

给你一个长度为 n 、下标从 0 开始的整数数组 batteryPercentages ,表示 n 个设备的电池百分比。

你的任务是按照顺序测试每个设备 i,执行以下测试操作:

如果 batteryPercentages[i] 大于 0:
增加 已测试设备的计数。
将下标在 [i + 1, n - 1] 的所有设备的电池百分比减少 1,确保它们的电池百分比 不会低于 0 ,即 batteryPercentages[j] = max(0, batteryPercentages[j] - 1)。
移动到下一个设备。
否则,移动到下一个设备而不执行任何测试。
返回一个整数,表示按顺序执行测试操作后 已测试设备 的数量。

示例 1:

输入:batteryPercentages = [1,1,2,1,3]
输出:3
解释:按顺序从设备 0 开始执行测试操作:
在设备 0 上,batteryPercentages[0] > 0 ,现在有 1 个已测试设备,batteryPercentages 变为 [1,0,1,0,2] 。
在设备 1 上,batteryPercentages[1] == 0 ,移动到下一个设备而不进行测试。
在设备 2 上,batteryPercentages[2] > 0 ,现在有 2 个已测试设备,batteryPercentages 变为 [1,0,1,0,1] 。
在设备 3 上,batteryPercentages[3] == 0 ,移动到下一个设备而不进行测试。
在设备 4 上,batteryPercentages[4] > 0 ,现在有 3 个已测试设备,batteryPercentages 保持不变。
因此,答案是 3 。
示例 2:

输入:batteryPercentages = [0,1,2]
输出:2
解释:按顺序从设备 0 开始执行测试操作:
在设备 0 上,batteryPercentages[0] == 0 ,移动到下一个设备而不进行测试。
在设备 1 上,batteryPercentages[1] > 0 ,现在有 1 个已测试设备,batteryPercentages 变为 [0,1,1] 。
在设备 2 上,batteryPercentages[2] > 0 ,现在有 2 个已测试设备,batteryPercentages 保持不变。
因此,答案是 2 。

提示:

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

解题思路一:模拟

class Solution:
    def countTestedDevices(self, batteryPercentages: List[int]) -> int:
        ans = 0
        for i in range(len(batteryPercentages)):
            if batteryPercentages[i] > 0 and batteryPercentages[i] > ans:
                ans += 1
        return ans

时间复杂度:O(n)
空间复杂度:O(1)

解题思路二: 一次遍历,简洁写法

在这里插入图片描述

class Solution:
    def countTestedDevices(self, batteryPercentages: List[int]) -> int:
        dec = 0
        for x in batteryPercentages:
            if x > dec:
                dec += 1
        return dec

时间复杂度:O(n)
空间复杂度:O(n)

解题思路三:0


时间复杂度:O(n)
空间复杂度:O(n)


创作不易,观众老爷们请留步… 动起可爱的小手,点个赞再走呗 (๑◕ܫ←๑)
欢迎大家关注笔者,你的关注是我持续更博的最大动力


原创文章,转载告知,盗版必究



在这里插入图片描述


在这里插入图片描述
♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠

相关推荐

  1. 2960. 统计测试设备

    2024-05-11 14:12:05       41 阅读
  2. LeetCode】每日一题:2960. 统计测试设备

    2024-05-11 14:12:05       30 阅读
  3. LeetCode 每日一题 ---- 【2960.统计测试设备

    2024-05-11 14:12:05       39 阅读
  4. LeetCode 2660. 保龄球游戏的获胜者:模拟

    2024-05-11 14:12:05       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-11 14:12:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-11 14:12:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-11 14:12:05       87 阅读
  4. Python语言-面向对象

    2024-05-11 14:12:05       96 阅读

热门阅读

  1. LeetCode hot100-34-G

    2024-05-11 14:12:05       32 阅读
  2. 如何在本地调试THUDM/chatglm2-6b大模型

    2024-05-11 14:12:05       35 阅读
  3. 保研机试之【构造二叉树】

    2024-05-11 14:12:05       35 阅读
  4. 软件测试应用技术--架构相关的注意事项

    2024-05-11 14:12:05       34 阅读
  5. 25、Flink 支持的数据类型及序列化详解

    2024-05-11 14:12:05       26 阅读
  6. 【图像超分】论文精读:Deep Image Prior(DIP)

    2024-05-11 14:12:05       36 阅读
  7. 【QEMU系统分析之实例篇(二十七)】

    2024-05-11 14:12:05       28 阅读
  8. MySQL变量的定义与使用(一)

    2024-05-11 14:12:05       33 阅读
  9. leetcode21-Merge Two Sorted Lists

    2024-05-11 14:12:05       33 阅读
  10. 单例模式(Singleton Pattern)

    2024-05-11 14:12:05       38 阅读
  11. Flask-Login 实现用户认证

    2024-05-11 14:12:05       30 阅读
  12. 投影与降维

    2024-05-11 14:12:05       34 阅读