题目浅析

  • 想查看原题可以点击题目链接

  • 简单地说,就是给一个一维数组,从左向右遍历操作,若当前的数字大于零,则对其之后的所有数字做减一操作,这算一次操作,求总共的操作数。

思路分享

代码解答(强烈建议自行解答后再看)

  • 参考题解
1
2
3
4
5
6
7
8
9
class Solution:
def countTestedDevices(self, batteryPercentages: List[int]) -> int:
ans = 0
have_cut = 0
for b in batteryPercentages:
if b + have_cut > 0:
have_cut -= 1
ans += 1
return ans