1996. The Number of Weak Characters in the Game
https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/
Python
Greedy
- Time: O(NlogN) # From the sort
- Space: O(1)
class Solution:
def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
properties.sort(key=lambda prop: (-prop[0], prop[1]))
max_defence, ans = 0, 0
for _, defence in properties:
if defence < max_defence:
ans += 1
max_defence = max(max_defence, defence)
return ans