题目地址:
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;}
}