[算法][差分][延迟相差][leetcode]2960. 统计已测试设备

题目地址:

https://leetcode.cn/problems/count-tested-devices-after-test-operations/description/


解法一:暴力解法

class Solution {
    public int countTestedDevices(int[] batteryPercentages) {
        //特殊条件判断
        if(null == batteryPercentages || batteryPercentages.length==0){
            return 0;
        }

        //记录结果
        int res=0;

        int n = batteryPercentages.length;

        for(int i=0; i<n;i++){
            if(batteryPercentages[i]>0){
                res++;
            }else{
                continue;
            }
            for(int j =i+1;j<n;j++){
               batteryPercentages[j]=Math.max(0, batteryPercentages[j] - 1);
            }
        }

        return res;
    }
}

解法二:差分法

  class Solution {

    public int countTestedDevices(int[] batteryPercentages) {
        //记录被减一的次数
        int d = 0;
        int res=0;
        for(int i : batteryPercentages){
            if(i-d>0){
                res++;
                d++;
            }
        }

        return res;
    }
}

相关推荐

  1. 2960. 统计测试设备

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

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

    2024-05-12 15:04:02       39 阅读
  4. 进化算法

    2024-05-12 15:04:02       18 阅读

最近更新

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

    2024-05-12 15:04:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-12 15:04:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-12 15:04:02       82 阅读
  4. Python语言-面向对象

    2024-05-12 15:04:02       91 阅读

热门阅读

  1. 云存储有哪些类型?

    2024-05-12 15:04:02       28 阅读
  2. Servlet的几种用法?

    2024-05-12 15:04:02       26 阅读
  3. C++原型模式

    2024-05-12 15:04:02       34 阅读
  4. 简述redis事务

    2024-05-12 15:04:02       31 阅读
  5. 华为校招机试 - 模拟汇编计算(20240508)

    2024-05-12 15:04:02       32 阅读
  6. linux内核debug(一)oops

    2024-05-12 15:04:02       32 阅读