fix:提莫攻击

上级 63f55300
"""
最大连续 1 的个数
"""
from typing import List
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
"""
试试贪心算法
:param nums:
:return:
"""
res = 0
count = 0
for num in nums:
if num == 0:
count = 0
continue
count += 1
res = max(res, count)
return res
if __name__ == '__main__':
result = Solution().findMaxConsecutiveOnes([1, 1, 0, 1, 1, 1])
print(result)
"""
提莫攻击
"""
from typing import List
class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
"""
贪心算法
:param timeSeries:
:param duration:
:return:
"""
if not timeSeries:
return 0
res = duration
for i in range(1, len(timeSeries)):
# 中毒时间取最小值,当有重叠时timeSeries[i] - timeSeries[i - 1]
res += min(duration, timeSeries[i] - timeSeries[i - 1])
return res
if __name__ == '__main__':
# result = Solution().findPoisonedDuration([1, 4], 2)
result = Solution().findPoisonedDuration([1, 2], 2) # 输出3
print(result)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册