思路:
跟大家作法差不多
就先排序然后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