Re: [闲聊] 每日leetcode

楼主: DJYOMIYAHINA (通通打死)   2024-07-13 11:56:38
虾J8乱写
最后zip unpack那边
因为空list会出错 搞了好久==
就先这样了
应该很多地方可以优化
def survivedRobotsHealths(self, positions: List[int], healths: List[int],
directions: str) -> List[int]:
n = len(positions)
idx = [i+1 for i in range(n)]
positions, healths, directions, idx = zip(*sorted(zip(positions, healths,
directions, idx)))
positions, healths, directions, idx = list(positions), list(healths),
list(directions), list(idx)
print(positions)
stk = []
ans = []
for i in range(n):
if directions[i] == "R":
stk.append(i)
else:
while stk:
if healths[stk[-1]] < healths[i]:
stk.pop()
healths[i] -= 1
elif healths[stk[-1]] == healths[i]:
stk.pop()
healths[i] = 0
break
else:
healths[stk[-1]] -= 1
break
if not stk and healths[i] > 0:
ans.append(i)
ans = ans + stk
ans = [(idx[i], healths[i]) for i in ans]
if ans:
_, ans = zip(*sorted(ans))
return list(ans)
else:
return []
作者: nozomizo (希霙)   2024-07-13 11:58:00
大师
作者: a114514 (a114514)   2024-07-13 12:00:00
你们写for循环都不会用挂号包起来吗
作者: oin1104 (是oin的说)   2024-07-13 12:00:00
大师
作者: Rushia (みけねこ的鼻屎)   2024-07-13 12:01:00
宝 py没括号
作者: sustainer123 (caster)   2024-07-13 12:01:00
哲维 这py
作者: a114514 (a114514)   2024-07-13 12:01:00
好吧 我只学过C++
作者: oin1104 (是oin的说)   2024-07-13 12:02:00
c++如果只有一行的话 好像也可以不用括号
作者: a114514 (a114514)   2024-07-13 12:05:00
是没错 但是我强迫症会发作

Links booklink

Contact Us: admin [ a t ] ucptt.com