Re: [閒聊] 每日leetcode

作者: sustainer123 (caster)   2024-07-13 15:38:54
思路:
跟大家作法差不多
就先排序然後stack
我是回傳那邊卡很久
想說問題在哪
後來發現題目有說明要求 靠北
Python Code:
class Solution:
def survivedRobotsHealths(self, positions: List[int], healths: List[int],
directions: str) -> List[int]:
n = len(positions)
original_indices = list(range(n))
zipped = sorted(zip(positions, healths, directions, original_indices))
positions, healths, directions, original_indices = zip(*zipped)
healths = list(healths)
r = []
for i in range(len(positions)):
if directions[i] == "R":
r.append(i)
else:
while r and healths[i] > 0:
if healths[i] == healths[r[-1]]:
healths[i] = 0
healths[r[-1]] = 0
r.pop()
elif healths[i] > healths[r[-1]]:
healths[i] -= 1
healths[r[-1]] = 0
r.pop()
else:
healths[i] = 0
healths[r[-1]] -= 1
survivors = [(original_indices[i], healths[i]) for i in range(n) if
healths[i] > 0]
survivors.sort()
res = [health for _, health in survivors]
return res
作者: CanIndulgeMe (CIM)   2024-07-13 15:41:00
技術大神
作者: smart0eddie (smart0eddie)   2024-07-13 16:21:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com