1996. 游戏中弱角色的数量

exiaohu 于 2022-01-28 发布

题目链接:1996. 游戏中弱角色的数量

为了防治出现攻击力相同的情况,让按攻击力递减、防御力递增排序。防御力如果小于之前记录的最大防御力,那么攻击力一定不可能相等,因为攻击力相等时防御力是递增的。

from typing import List


class Solution:
    def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
        count, max_defense = 0, float('-inf')
        for atk, dfs in sorted(((atk, dfs) for atk, dfs in properties), key=lambda c: (c[0], -c[1]), reverse=True):
            if max_defense > dfs:
                count += 1
            else:
                max_defense = dfs

        return count