Leetcode 问题是https://leetcode.com/problems/eliminate-maximum-number-of-monsters/ 我的代码如下:
class Solution(object):
def eliminateMaximum(self, dist, speed):
"""
:type dist: List[int]
:type speed: List[int]
:rtype: int
"""
mapped = []
killed = 0
for i, v in enumerate(zip(dist, speed)):
mapped.append([v[0]/v[1], v[0], v[1]])
mapped.sort(key=lambda x: x[0])
iterator = iter(mapped)
while True:
try:
next(iterator)[1] = -1
killed += 1
except StopIteration:
return killed
for v in mapped[killed:]:
v[1] = v[1]-v[2]
if v[1] <= 0:
return killed
sol = Solution()
r = sol.eliminateMaximum([4,8,6,8,2,7,4], [1,3,3,1,10,1,1])
print(r)
我的输出是预期的 4,但是 leetcode 得到 2 并告诉我这是一个错误的答案。
• 在 leetcode 中完成的任务不起作用,但它在 Pycharm 中有效 [关闭]
• 为什么这个 python 代码抛出 OverflowError?
• Char 23:警告:未指定与字符串文字的比较结果(改用显式字符串比较函数)[-Wstring-compare]
• 使用这两种方法在二维矩阵中查找目标值的时间复杂度是否相同?
• 为什么“set(a) and set(b)”与“set(a) & set(b)”在 python 中给出不同的结果?
• set.remove() 是否导致我的代码速度大幅下降?
• 为什么 void & {} 不会被推断为从不在 TypeScript 中输入?
• 为什么在数据库查询中使用带有特殊字符的请求参数无法找到结果,但与固定值相同的字符串却可以正常工作?